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
原创粉丝点击