?
?
一、绑定事件的方法:
1、方法一:UI组件事件属性调用
main.xml:
<Button
? ? ? ? android:id="@+id/button1"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:onClick="forWard"
? ? ? ? android:text="跳转" />
<EditText
? ? ? ? android:id="@+id/editText2"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? >
?
Main.java:
protected void onCreate(Bundle savedInstanceState) {
? ? ? ? ? ? ?super.onCreate(savedInstanceState);
? ? ? ? ? ? ?setContentView(R.layout.main);
??
? ? ? ? ? ? ?/**接收调用Input.java中startActivity方法传递过来的数据*/
? ? ? ? ? ? ?EditText editText2=(EditText)this.findViewById(R.id.editText2);
? ? ? ? ? ? ?Intent intent = getIntent();
? ? ? ? ? ? ?String text= intent.getStringExtra(Input.EXTRA_MESSAGE);
? ? ? ? ? ? ?if(text!= null){
? ? ? ? ? ? ? ? ?editText2.setText(text);
? ? ? ? ? ? ?}?
}
?
? /**UI组件事件属性调用*/
public void forWard(View view){
? ? ? ? ? ? ? Intent intent = new Intent(Main.this,Input.class);
? ? ? ? ? ? ? startActivity(intent);//定义好的intent对象启动一个Activity?跳向Input.java
}
?
input.xml:
<EditText
? ? ? ? android:id="@+id/editText1"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_weight="1"
? ? ? ? android:ems="10"
? ? ? ? android:hint="请输入"
? ? ? ? android:inputType="textPersonName" >
? ? </EditText>
?
? ? <Button
? ? ? ? android:id="@+id/button1"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_weight="1"
? ? ? ? android:text="返回数据"?
? ? ? ? android:onClick="sendMessage"
? ? ? ? />
?
Input.java:
? ? static String EXTRA_MESSAGE="text";
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.input);
? ? }
?
? ? /**UI组件事件属性调用:startActivity*/
? ? public void sendMessage(View view) {
? ? ? ? ?// 获取用户在该页面输入的值,传送给Main.java
? ? ? ? ?Intent intent = new Intent(this, Main.class);//MainActivity向DisplayMessageActivity传送
? ? ? ? ?EditText editText = (EditText) findViewById(R.id.editText1);
? ? ? ? ?String text = editText.getText().toString();
? ? ? ? ?intent.putExtra("text", text);//向intent中存放名为EXTRA_MESSAGE的数据
? ? ? ? ?startActivity(intent);
? ? }
?
2、方法二:后端代码向UI组件动态绑定事件
?
Main.java:
protected void onCreate(Bundle savedInstanceState) {
? ? super.onCreate(savedInstanceState);
? ? setContentView(R.layout.main);
?
? ? /**后端向UI组件动态绑定事件*/
? ? Button button =(Button)this.findViewById(R.id.button1);
? ? button.setOnClickListener(new View.OnClickListener() {
? ? ? ? @Override
? ? ? ? public void onClick(View arg0) {?
? ? ? ? ? ? Intent intent = new Intent(Main.this,Input.class);
? ? ? ? ? ? startActivity(intent);//定义好的intent对象启动一个Activity
? ? ? ? }
? ? });
?
? ? /**接收调用startActivity方法,其他Activity传递过来的数据*/
? ? EditText editText2=(EditText)this.findViewById(R.id.editText2);
? ? Intent intent = getIntent();
? ? String text = intent.getStringExtra(Input.EXTRA_MESSAGE);
? ? if(text != null){
? ? ? ? editText2.setText(text);
? ? }?
}
?
Input.java:
static String EXTRA_MESSAGE="text";
@Override
protected void onCreate(Bundle savedInstanceState) {
? ? super.onCreate(savedInstanceState);
? ? setContentView(R.layout.input);
?
? ? /**后端向UI组件动态绑定事件*/
? ? Button button =(Button)this.findViewById(R.id.button1);
? ? button.setOnClickListener(new View.OnClickListener() {
? ? ? ? @Override
? ? ? ? public void onClick(View arg0) {
?
? ? ? ? ? ? EditText editText =(EditText)findViewById(R.id.editText1);
? ? ? ? ? ? String text = editText.getText().toString();
?
? ? ? ? ? ? /**Main中startActivity跳转过来,向Intent中放数据传递给Main.java*/
? ? ? ? ? ? // 获取用户在该页面输入的值,传送给另外一个页面
? ? ? ? ? ? Intent intent = new Intent(Input.this, Main.class);//MainActivity向DisplayMessageActivity传送
? ? ? ? ? ? intent.putExtra("text", text);//向intent中存放名为EXTRA_MESSAGE的数据
? ? ? ? ? ? startActivity(intent);?
? ? ? ? }
? ? });
}
?
二、startActivity、startActivityForResult区别
?
Main.java:
protected void onCreate(Bundle savedInstanceState) {
? ? super.onCreate(savedInstanceState);
? ? setContentView(R.layout.main);
?
? ? /**后端向UI组件动态绑定事件*/
? ? Button button =(Button)this.findViewById(R.id.button1);
? ? button.setOnClickListener(new View.OnClickListener() {
? ? ? ? @Override
? ? ? ? public void onClick(View arg0) {?
? ? ? ? ? ? Intent intent = new Intent(Main.this,Input.class);
? ? ? ? ? ? //定义好的intent对象启动一个带返回的Activity(用startActivityForResult返回时不会再调用onCreate方法)
? ? ? ? ? ? startActivityForResult(intent, 0);
? ? ? ? }
? ? });?
}?
?
/**接收调用startActivityForResult方法返回的数据*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
? ? super.onActivityResult(requestCode, resultCode, data);
? ? if(requestCode==0){
? ? ? ? if(resultCode == Activity.RESULT_OK){
? ? ? ? ? ? SharedPreferences preferences = getSharedPreferences("Text", 0);
? ? ? ? ? ? EditText editText2=(EditText)this.findViewById(R.id.editText2);
? ? ? ? ? ? editText2.setText(preferences.getString("text", null));
? ? ? ? }
? ? }
}?
?
?
Input.java:
static String EXTRA_MESSAGE="text";
@Override
protected void onCreate(Bundle savedInstanceState) {
? ? super.onCreate(savedInstanceState);
? ? setContentView(R.layout.input);
?
? ? /**后端向UI组件动态绑定事件*/
? ? Button button =(Button)this.findViewById(R.id.button1);
? ? button.setOnClickListener(new View.OnClickListener() {
? ? ? ? @Override
? ? ? ? public void onClick(View arg0) {?
? ? ? ? ? ? EditText editText =(EditText)findViewById(R.id.editText1);
? ? ? ? ? ? String text = editText.getText().toString();
?
? ? ? ? ? ??/**Main中startActivityForResult跳转过来,向SharedPreferences中放数据传递给Main.java*/
? ? ? ? ? ?SharedPreferences preferences = getSharedPreferences("Text", 0);
? ? ? ? ? ?SharedPreferences.Editor edites = preferences.edit();
? ? ? ? ? ?edites.putString("text", text);
? ? ? ? ? ?if(edites.commit()){
? ? ? ? ? ? ? ?setResult(Activity.RESULT_OK);
? ? ? ? ? ?}
? ? ? ? ? ?finish();
? ? ? ? }
? ? });
}
?
注意:
UI组件事件属性调用,比较startActivity和startActivityForResult的区别,只是把动态绑定中的相应代码拿出放到一个方法中即可