android 實現頁面跳轉及數據的傳遞和返回


1.實現效果:

原始界面:     ----傳輸數據----------> 填寫數據后,點擊計算后界面-----返回數據----->點擊返回按鈕后,回到上一個頁面,依舊能夠保留之前保持的數據

                                 

2.實現代碼:

a.兩個布局文件:

activity_main.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.example.activity_return.MainActivity">
<TextView
android:textSize="30sp"
android:layout_marginTop="30dp"
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title" />
<LinearLayout
android:layout_marginTop="30dp"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:textSize="20sp"
android:text="@string/sex"
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioGroup
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:checked="true"
android:layout_marginLeft="10dp"
android:id="@+id/rb1"
android:text="@string/man"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:layout_marginLeft="30dp"
android:id="@+id/rb2"
android:text="@string/woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_marginLeft="50dp"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="@string/height"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et"
android:layout_marginLeft="10dp"
android:background="@drawable/bg_edittext"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:layout_marginLeft="10dp"
android:text="cm"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:layout_marginTop="50dp"
android:layout_marginLeft="100dp"
android:id="@+id/bt"
android:text="@string/calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

new_activity.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/text"
android:layout_marginLeft="50dp"
android:layout_marginTop="50dp"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="返回"
android:layout_marginTop="20dp"
android:layout_marginLeft="50dp"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

b.兩個activity.java文件:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private EditText et;
private RadioButton rb1;
private RadioButton rb2;
private Button bt;
private Double height;
private String sex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//實例化控件
initData();
//實現跳轉
jump();
}
private void initData(){
et=(EditText)findViewById(R.id.et);
bt=(Button)findViewById(R.id.bt);
rb1=(RadioButton)findViewById(R.id.rb1);
rb2=(RadioButton)findViewById(R.id.rb2);

}
private void jump(){
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str=et.getText().toString();
//一定要注意這一步的判斷,因為用戶可能沒有填寫身高就提交了,那么這種情況下會導致程序奔潰
if(!str.equals("")) height=Double.parseDouble(et.getText().toString());
else{
et.setHint("請輸入身高");
return;
}
//那么對於這種情況,我們可以在布局文件中先設置某個按鈕默認的checked為true,然后根據用戶來更改
if(rb1.isChecked()){
sex="M";
}else{
sex="F";
}
Intent intent=new Intent();
intent.setClass(MainActivity.this,New_Activity.class);
//利用bundle來存取數據
Bundle bundle=new Bundle();
bundle.putDouble("height",height);
bundle.putString("sex",sex);
//再把bundle中的數據傳給intent,以傳輸過去
intent.putExtras(bundle);
startActivityForResult(intent,0);
}
});
}
//這里是設置獲取從第二頁面中返回的數據,如果我們沒有設置這個的話,我們返回該頁面,那么數據都會清空
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){//如果是返回的標識
//獲取數據
Bundle bundle=data.getExtras();
sex=bundle.getString("sex");
height=bundle.getDouble("height");
//保留之前的數據
if(sex.equals("M")){
rb1.setChecked(true);
}else{
rb2.setChecked(true);
}
String str=height.toString();
et.setText(str);
}
}
}
new_activity.java:
public class New_Activity extends Activity {
private TextView textView;
private String sex;
private String sexText;
private Double height;
private String weight;
private Button button;
private Intent intent;
private Bundle bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_layout);
initData();
//設置返回上一個頁面的數據
setBackData();
}
private void initData(){
textView=(TextView)findViewById(R.id.text);
button=(Button)findViewById(R.id.button1);
//獲取上個頁面傳輸過來的數據放在intent中
intent=this.getIntent();
bundle=intent.getExtras();
sex=bundle.getString("sex");
height=bundle.getDouble("height");
if(sex.equals("M")){
sexText="男性";
}else{
sexText="女性";
}
getWeight();
}
private void getWeight(){
if(sex.equals("M")){
weight=(height-80)*0.7+"";
}else{
weight=(height-70)*0.6+"";
}
}
private void setBackData(){
textView.setText("你是一位"+sexText+"\n你的身高是"+height+"厘米\n你的標准體重是"+weight+"公斤");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//因為我們在initData中已經將傳輸過來的數據放在intent中,所以這里我們直接用intent即可
setResult(RESULT_OK,intent);
finish();
}
});
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM