zoukankan      html  css  js  c++  java
  • startActivityForResult的使用和用法

    startActivityForResult的使用和用法

    startActivityForResult 和 onActivityResult在activity间传递数据
    
    AndroidManifest.xml
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:name=".KakuLogerActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            
            <activity android:name=".LaunchActivity" 
                      android:label="@string/app_name2" >
            </activity>
        </application>
    
    主Activity:
    public class KakuLogerActivity extends Activity {
        
        TextView phoneNumber;
        EditText iNameField;
        int REQUEST_CODE = 0;
        
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            Button launchBtn = (Button)findViewById(R.id.btnLaunchActivity);
            iNameField = (EditText)findViewById(R.id.inamefield);
            phoneNumber = (TextView)findViewById(R.id.phone_number);
    
            launchBtn.setOnClickListener(new OnClickListener(){
                @Override  
                public void onClick(View v) {  
                    Intent newIntent = new Intent(KakuLogerActivity.this, LaunchActivity.class);
                    // 传递参数
                    newIntent.putExtra("name", iNameField.getText().toString()); 
                    // 开始一个新的 Activity等候返回结果
                    startActivityForResult(newIntent, REQUEST_CODE);
                    Log.i("clicked", "open the LaunchActivity page!!!!");
                }
            });
        }
    
        @Override
        // 当结果返回后判断并执行操作
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
            super.onActivityResult(requestCode, resultCode, intent);
            if (requestCode == REQUEST_CODE) {
                if (resultCode == RESULT_OK) {
                    Bundle extras = intent.getExtras();
                    if (extras != null) {
                        phoneNumber.setText("Phone #: "
                                + extras.getString("phonenumber"));
                    }
                }
            }
        }
    }
    LaunchActivity:
    public class LaunchActivity extends Activity {
        
        EditText phoneNumber;
        
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout2);
            Button btn = (Button)findViewById(R.id.btnRtrnActivity);
            TextView oNameField = (TextView)findViewById(R.id.oNameMessege);
            phoneNumber = (EditText)findViewById(R.id.iTelephoneField);
            Log.i("launchActivity", "opened the LaunchActivity page!!!!");
            // 取得前一个画面的传入值
            Bundle extras = getIntent().getExtras();
            oNameField.setText("Hello " + extras.getString("name"));
    
            btn.setOnClickListener(new OnClickListener(){  
                public void onClick(View v) {
                     // 设置返回数据
                    Bundle bundle = new Bundle();
                    bundle.putString("phonenumber", phoneNumber.getText().toString());
                    Intent intent = new Intent();
                    intent.putExtras(bundle);
                    // 返回intent
                    setResult(RESULT_OK, intent);
                    finish();
                }
            });
        }
    }
  • 相关阅读:
    改变多行文本字符串的缩进
    多线程
    python基本语法2.5--字符串的相关操作
    python基本语法2.4---汉诺塔的递归
    python基本语法2.3--函数及参数传递
    python基本语法2.2--函数名当作变量传递
    python基本语法2.1--if判断和while,for循环
    AlexNet源码
    python基本语法1.4--初识爬虫
    python基本语法1.5--调用numpy库的性能影响
  • 原文地址:https://www.cnblogs.com/yejiurui/p/3200199.html
Copyright © 2011-2022 走看看