From one Activity To other Activity

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值