首页 > 代码库 > Activity系列讲解---返回结果的处理

Activity系列讲解---返回结果的处理

  设想一下:由当前Activity跳转到其它Activity,从其它Activity再返回到当前Activity时,如何获取其它Activity存放的数据?下面用一个例子讲解,

    点击selsect按钮跳转到另一界面,同时选择一个电话号码;然后将这个电话号码带回来显示在当前界面的EditText上。

技术分享

1.代码实现:

(1)ResultActivity.class 

/**
*结果界面
*/

public class ResultActivity extends AppCompatActivity {

    private static final int REQUEST_CODE_1 = 0x1;//请求码
    private EditText et;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        et = (EditText) findViewById(R.id.et_phone_data);

    }

    /**
     * 选择一个电话号码
     * @param view
     */
    public void selectClick(View view){
        Intent intent = new Intent(this,PhoneListActivity.class);
        //参数:intent,请求编码
        startActivityForResult(intent,REQUEST_CODE_1);

    }

    //重写该方法来处理返回的结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==REQUEST_CODE_1 && resultCode==RESULT_OK){
            String nums = data.getStringExtra("no");
            et.setText(nums);
        }
    }

    /**
     * 拨打电话--弹出手机里拨打电话的界面
     * @param view
     */
    public void CallClick(View view){
        String phoneNum = et.getText().toString();
        Intent intent = new Intent();

        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:"+phoneNum));
        startActivity(intent);
    }

}

 

(2)PhoneListActivity.class

/**
*选择电话号码界面
*/

public class PhoneListActivity extends AppCompatActivity {
    private ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_list);

        lv = (ListView) findViewById(R.id.lv_listView_phone);

        final String[] numbers = {"18253563602","18810623657","15949822985","15854388317","15275323389","13521205590"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,numbers);
        lv.setAdapter(adapter);
        //每一项的单击事件
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String no = numbers[position];
                Intent intent = new Intent();
                intent.putExtra("no",no);
                setResult(RESULT_OK,intent);//设置返回码
                finish();//结束当前界面
            }
        });
    }


}

  

(3)activity_result.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.langdon.taiyang.androidtest.activity.ResultActivity">

    <EditText
        android:id="@+id/et_phone_data"
        android:hint="请选择一个电话号码"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/bt_phone_select"
        android:text="select"
        android:onClick="selectClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/bt_phone_call"
        android:text="call"
        android:onClick="CallClick"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

  

(4)activity_phone_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_phone_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.langdon.taiyang.androidtest.activity.PhoneListActivity">
<ListView
    android:id="@+id/lv_listView_phone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</RelativeLayout>

 2.运行结果:(选择15275323389的电话号码,并将数据返回,显示在当前界面的EditText上)

技术分享 技术分享

Activity系列讲解---返回结果的处理