2013年1月28日 星期一

使用Bundle 傳遞參數



本程式使用Bundle物件,由main_activity傳遞參數到result:
首先設計一個 main_activity.xml
包含兩個Radiobutton、一個EditText、以及一個Button




<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=".MainActivity" >

  
<LinearLayout 
    android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
    >
    <RadioButton  
         android:id="@+id/rb1"
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:text="男生"
        />
      <RadioButton  
         android:id="@+id/rb2"
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:text="女生"
        />
       <EditText 
           android:id="@+id/et"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="0"
            /> 
        </LinearLayout>
      <Button
           android:id="@+id/bt1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Submit"
         />
      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"               
        android:text="@string/hello_world" />
  
    
</LinearLayout>



目的地Result.xml 包含一個textview,id 為Textview


MainActivity.java 程式碼:

package com.example.p0123;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends Activity {
 private Button bt1;
 private EditText et1;
 private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1=(Button)findViewById(R.id.bt1);
et1=(EditText)findViewById(R.id.et);
tv=(TextView)findViewById(R.id.tv);
// if (et1.getText().toString().length()>0)
// tv.setText(" "+age);
// else 
// age=1;
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
 Integer age=0;
String sex="";
RadioButton rb1=(RadioButton)findViewById(R.id.rb1);
RadioButton rb2=(RadioButton)findViewById(R.id.rb2);

if (rb1.isChecked())
{
sex="M";
}else {sex="F";}
// tv.setText(sex);
// age=100;
age=Integer.parseInt(et1.getText().toString());
Intent intent =new Intent();
intent.setClass(MainActivity.this, Resultform.class);
Bundle bundle=new Bundle();
bundle.putInt("age", age);
bundle.putString("sex", sex);
intent.putExtras(bundle);
startActivity(intent);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}


Result.java
package com.example.p0123;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Resultform extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
TextView tv=(TextView)findViewById(R.id.tv);
Bundle bundle=this.getIntent().getExtras();
Integer age=bundle.getInt("age");
String sex=bundle.getString("sex");
tv.setText(sex+":"+age);
}
}


結果如下




沒有留言:

張貼留言