From one Activity To other Activity
来源:互联网 发布:java贪吃蛇游戏源代码 编辑:程序博客网 时间:2024/05/29 14:44
1.layout 文件 content_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/content_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/activity_main" tools:context="com.sh.appfirst.MainActivity"> <EditText android:id="@+id/message" android:layout_width="0dp" android:layout_weight="2" android:layout_height="wrap_content" android:hint="@string/edit_message" /> <Button android:text="@string/button_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="sendMessage" />>
2.java 文件
MainActivity.java
public class MainActivity extends AppCompatActivity {// public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } /* 是public函数 无返回值 参数唯一(为View类型,代表被点击的视图) */ public void sendMessage(View view) {// Toast.makeText(this, "clicked button", Toast.LENGTH_SHORT).show(); /* 在这个Intent构造函数中有两个参数: 第一个参数是Context(之所以用this是因为当前Activity是Context的子类) 接受系统发送Intent的应用组件的Class(在这个案例中,指将要被启动的activity)。 Android Studio会提示导入Intent类。 */ Intent intent = new Intent(this,DisplayMessageActivity.class); EditText editText = (EditText)findViewById(R.id.message);// 把EditText的文本内容关联到一个本地 message 变量,并使用putExtra()方法把值传给intent. String message = editText.getText().toString(); /* putExtra:将数据以key:value的形式放入一个Parcelable对象中,直接由Intent对象携带,适合少量数据。 setData:将数据以数据流的方式传输,Intent接收后再单独接收Data部分,适合数据量较大的数据传输,如文件或图片等。 */ intent.putExtra("EXTRA_MESSAGE",message); //Intent可以携带称作 extras 的键-值对数据类型。 putExtra()方法把键名作为第一个参数,把值作为第二个参数。 startActivity(intent); } }
3.另一个Activity 文件DisplayMessageActivity.java
public class DisplayMessageActivity extends AppCompatActivity { /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ private GoogleApiClient client; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_message);// 得到intent 并赋值给本地变量. Intent intent = getIntent();// 调用 getStringExtra()提取从 MyActivity 传递过来的消息.// String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); String message = intent.getStringExtra("EXTRA_MESSAGE"); TextView textView = new TextView(this); textView.setTextSize(40); textView.setText(message); RelativeLayout layout = (RelativeLayout)findViewById(R.id.content); layout.addView(textView); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); } }
0 0
- From one Activity To other Activity
- Allowing Other Apps to Start Your Activity
- Allowing Other Apps to Start Your Activity
- Allowing Other Apps to Start Your Activity
- 17Allowing Other Apps to Start Your Activity
- Android Training--Allowing Other Apps to Start Your Activity
- From Activity
- Android Activity – From One Screen To Another Screen从一个屏幕切换到另一屏幕,Intent
- angular transfer msg from one controller to the other controller by $scope.$on()
- android-Allowing Other Apps to Start Your Activity(App之间进行交互)
- Functional MRI (second edition) -- 6. From Neuronal to Hemodynamic Activity
- Spark: From Zero To One
- activity to View
- Unable to instantiate activity
- Unable to stop activity
- Unable to instantiate activity
- not attached to activity
- Activity
- dbug
- 前端传数据到mysql乱码问题解决方案
- badboy脚本开发事件
- PowerDesigner:导出SQL脚本
- 秒杀系统架构优化思路
- From one Activity To other Activity
- WebView加载图片超过屏幕大小的问题
- day88_shiro_2
- vim编辑器
- 机器智能相关资料
- mysql中isnull,ifnull,nullif的用法
- 工作流-流程部署
- mAP在计算机视觉中的应用
- Java获取硬盘,CPU等信息