2. 第一个Activity(MainActivity.java)
package org.me.android_transferparam; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { private Button nextButton; private EditText nameEdit; private EditText phoneEdit; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); //载入所有的Widget loadWidget(); nextButton.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View view){ //传送参数到下一个Actitity deliverToNextActitity(); } }); } public void deliverToNextActitity(){ //建立一个bundle物件,并将要传递的参数放到bundle里 Bundle bundle = new Bundle(); bundle.putString("name", nameEdit.getText().toString()); bundle.putString("phone", phoneEdit.getText().toString()); Intent intent = new Intent(); //设定下一个Actitity intent.setClass(this, SecondActivity.class); intent.putExtras(bundle); //开启Activity startActivity(intent); } private void loadWidget(){ nextButton = (Button) findViewById(R.id.nextButton); nameEdit = (EditText) findViewById(R.id.nameEdit); phoneEdit = (EditText) findViewById(R.id.phoneEdit); } }
3. 第二个Activity(SecondActivity.java)
package org.me.android_transferparam; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class SecondActivity extends Activity { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.second); TextView content = (TextView) findViewById(R.id.content); Button backButton = (Button) findViewById(R.id.backButton); //取得前一个Activity传过来的Bundle物件 Bundle bundle = getIntent().getExtras(); content.setText("姓名:"+bundle.getString("name")+" 电话:"+bundle.getString("phone")); backButton.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View view){ //结束此Activity finish(); } }); } }
4. main.xml(Layout)
5. second.xml(Layout)
"
6. 程式启动及输入参数之後的画面
沒有留言:
張貼留言