今天介绍一个安卓使用意图(Intent)传递数据并返回结果的使用。
下面这个例子是一个加法的计算器,通过写入被加数和加数,然后点击按钮跳转到另一个页面,并将两个数传到这个页面;在该页面填写计算的结果,点击按钮回到第一个页面并将结果传回到第一个页面上。
整体思路:从第一个活动向第二个活动传递两个数据,开启活动的方式设置请求标记;第二个活动收到这两个数据并将计算的·结果传递给第一个活动,设置结果标记,关闭第二个活动;随着第二个活动的消失,第一个活动显示在用户面前,第一个活动通过判断请求标记和结果标记的正确性之后设置结果数据。
activity_main.xml文件:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/one"
android:layout_height="wrap_content"
android:layout_width="80dp"
/>
<TextView
android:id="@+id/one1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
<EditText
android:id="@+id/two"
android:layout_height="wrap_content"
android:layout_width="80dp"
/>
<TextView
android:id="@+id/two2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="=" />
<EditText
android:id="@+id/result"
android:layout_height="wrap_content"
android:layout_width="80dp"
/>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="计算结果"
/>
</LinearLayout>
activity_other.xml文件:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回结果"
/>
MainActivity.java文件:
private Button button;
private static final int REQUESTCODE=1;
private EditText one,two,result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button);
one=(EditText)findViewById(R.id.one);
two=(EditText)findViewById(R.id.two);
result=(EditText)findViewById(R.id.result);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int a=Integer.parseInt(one.getText().toString());
int b=Integer.parseInt(two.getText().toString());
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
intent.putExtra("a", a);
intent.putExtra("b", b);
//启动activity的时候必须调用这个方式,才能得到返回结果
startActivityForResult(intent, REQUESTCODE);//表示返回结果
}
});
}
//用于接收从第二个activity中回传的值
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==2){//这里的标志位(2)要与第二个页面的标志位保持一致
if(requestCode==REQUESTCODE){
int three=data.getIntExtra("three", 0);
result.setText(String.valueOf(three));
}
}
}
OtherActivity.java文件:
private Button button;
private TextView textView;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
button=(Button)findViewById(R.id.button);
textView=(TextView)findViewById(R.id.msg);
editText=(EditText)findViewById(R.id.edit);
Intent intent=getIntent();
int a=intent.getIntExtra("a", 0);
int b=intent.getIntExtra("b", 0);
textView.setText(a+"+"+b+"="+"?");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent();//定义一个意图,用于存放一个整型变量three
int three=Integer.parseInt(editText.getText().toString());
intent.putExtra("three", three);
//通过intent对象返回结果,setResult方法
setResult(2,intent);//返回第一个页面的标志位,这个标志位置和第一个页面的标志位保持一致
finish();//结束当前activity的生命周期,这样就可以把值回传给第一个页面
}
});
}
注意:不要忘记注册新创建的activity。