当前位置:操作系统 > 安卓/Android >>

Android Activity返回结果startActivityForResult函数

 

点击第一个按钮,然后再第二个页面中输入文字,就会在第一个的EditText中出现在第二个Activity中输入的文字,、

代码一看就能明白startActivityForResult函数

第一个Activity

 

import android.app.Activity; 

import android.content.Intent; 

import android.os.Bundle; 

import android.util.Log; 

import android.view.View; 

import android.view.View.OnClickListener; 

import android.widget.Button; 

import android.widget.EditText; 

 

public class DataBackActivity extends Activity { 

    /** Called when the activity is first created. */ 

    private static final int REQUESTCODE=1; 

    private Button button; 

    private EditText editText; 

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.main); 

        button=(Button) findViewById(R.id.button1); 

        editText=(EditText) findViewById(R.id.editText1); 

        button.setOnClickListener(new MyButtonLIstener()); 

    } 

    class MyButtonLIstener implements OnClickListener{ 

 

        @Override 

        public void onClick(View v) { 

            // TODO Auto-generated method stub 

            Intent intent=new Intent(); 

            intent.setClass(DataBackActivity.this, To.class); 

            startActivityForResult(intent, REQUESTCODE); 

        }    

    }   www.zzzyk.com

    /* (non-Javadoc)

     * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)

     */ 

    @Override 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

        // TODO Auto-generated method stub 

        if (requestCode==REQUESTCODE) { 

            switch (resultCode) { 

            case RESULT_OK: 

                String dateString=data.getExtras().getString("Da"); 

                Log.i("----------->", dateString); 

                editText.setText(dateString); 

                break; 

 

            default: 

                break; 

            } 

        } 

         

         

        super.onActivityResult(requestCode, resultCode, data); 

    } 

     

 

第二个Activity

 

package rw.data; 

 

import android.app.Activity; 

import android.content.Intent; 

import android.os.Bundle; 

import android.view.View; 

import android.view.View.OnClickListener; 

import android.widget.Button; 

import android.widget.EditText; 

 

public class To extends Activity { 

    /** Called when the activity is first created. */ 

  

    private Button button; 

    private EditText editText; 

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.to); 

        button=(Button) findViewById(R.id.button1); 

        editText=(EditText) findViewById(R.id.editText1); 

        button.setOnClickListener(new MyButtonLIstener()); 

    } 

    class MyButtonLIstener implements OnClickListener{ 

 

        @Override 

        public void onClick(View v) { 

            // TODO Auto-generated method stub 

            Intent intent=new Intent(); 

            intent.putExtra("Da", editText.getText().toString()); 

            To.this.setResult(RESULT_OK, intent); 

            To.this.finish(); 

        }     

    } 

 

\

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,