标签:
首先是布局文件,如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <EditText android:id="@+id/edit_message" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="Hello World!" /> <Button android:id="@+id/send_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send" android:onClick="sendMessage" /> <!--定义onclick函数--> </LinearLayout>
package com.example.administrator.helloworld; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class MainActivity extends AppCompatActivity { //发送Intent对应字符串内容的key public static final String Intent_key="MESSAGE"; //EditText private EditText editText =null; private void initView(){ editText= (EditText) findViewById(R.id.edit_message); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置布局 setContentView(R.layout.activity_main); initView(); } //发送消息,启动secondActivity! public void sendMessage(View view){ Intent intent = new Intent(this,secondActivity.class); String text =editText.getText().toString(); intent.putExtra(Intent_key,text); startActivity(intent); } }
package com.example.administrator.helloworld; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class secondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent =getIntent(); String text =intent.getStringExtra(MainActivity.Intent_key); TextView textView =new TextView(this); textView.setTextSize(40); textView.setText(text); // Set the text view as the activity layout setContentView(textView); } }
android入门,activity跳转,并传递message
标签:
原文地址:http://www.cnblogs.com/zhoudayang/p/5257593.html