Tuesday, September 16, 2014

Android之Intent與Activity傳值

選擇頁



首先定義兩個RadioButton和一個Button

定義RadioButton的狀態
boolean btn1_state = false;
boolean btn2_state = false;

這裡注意需要加final
        Button sendBtn = (Button)findViewById(R.id.send);
        final RadioButton btn1 = (RadioButton)findViewById(R.id.rbtn1);
        final RadioButton btn2 = (RadioButton)findViewById(R.id.rbtn2);

        RadioGroup rg =(RadioGroup)findViewById(R.id.radiogroup1);


監察選擇了那個RadioButton

  rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId == btn1.getId()){
btn1_state = true;
btn2_state = false;
}else if(checkedId == btn2.getId()){
btn1_state = false;
btn2_state = true;
}
}

});

最後就是發送按鈕
 sendBtn.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
   // TODO Auto-generated method stub
      Intent intent = new Intent();
      if(btn1_state == true){
        //傳送RadioButton1的Text到另一個Activity
intent.putExtra("abc",btn1.getText().toString());
        //這裡的abc是RadioButton1中Text的名字,寫成123或zxcv都可以,
      }else if(btn2_state == true){
intent.putExtra("abc", btn2.getText().toString());
        //RadioButton2中的Text需與RadioButton1一樣
}
      //MainActivity是此Activity,而page_two則是另一個Activity
      intent.setClass(MainActivity.this, page_two.class);
      startActivity(intent);

   }
       
});

另一個Activity中

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.page_two);

this.setContentView(R.layout.page_two);

Bundle bundle = this.getIntent().getExtras();
                                      //取得一個名字為abc的String
String str = bundle.getString("abc");


TextView tv1 = (TextView)findViewById(R.id.tv1);
tv1.setText(str);
}

UI佈局

MainActivity:

  <RadioGroup  
        android:id="@+id/radiogroup1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:orientation="vertical"  
        android:layout_x="3px"  
    >  
    
    <RadioButton
        android:id="@+id/rbtn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="78dp"
        android:text="abc"
        android:textColor="#ffffff"
            />

    <RadioButton
        android:id="@+id/rbtn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dp"
        android:text="123"
        android:textColor="#ffffff"
            />

        </RadioGroup>  
    
     <Button
         android:id="@+id/send"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="send"
         android:textColor="#ffffff"
         />


page_two:

  <TextView 
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="100px"
        android:textColor="#ffffff"
        />

最後就是AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.intentdemo2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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="page_two"></activity>
    </application>

</manifest>

運行畫面:

No comments:

Post a Comment