Android之Intent(二)
阿新 • • 發佈:2018-06-08
Android Intent 前一篇(Android之Intent(一))講解過A(它用來調起B界面,這裏先理解為父界面) 可以向B(這裏先理解為子界面), 本篇正好是一個逆反的過程 , 即B向A傳遞數據。
2-1‘ (RadioButton)r_male : 男
2-2‘ (RadioButton) r_famale : 女
3‘ (Button) btn_ok : 關閉按鈕(向A界面發送信息)
如下圖:
一 : 界面設計
① ,A(activity_main.xml)
重要控件:
1‘ (TextView)txt_infos : 用來顯示從B交界面傳過來的信息 , 默認值為XXXXXX
2’ (Button)btn_go_A : 用來跳轉至B界面
如下圖:
② , B(activity_b2.xml)
重要控件
1‘ (EditText)in_name : 用來使用戶輸入文本
2’ (RadioGroup)rg_sex : 單選組用來選擇性別
2-2‘ (RadioButton) r_famale : 女
3‘ (Button) btn_ok : 關閉按鈕(向A界面發送信息)
如下圖:
二 : 代碼設計
①,A :
package com.example.kayer.aas; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private TextView txt_infos = null; private Button btn_go_A = null; final int REQ_GET_RESULT_4_B = 100; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.init(); } private void init(){ this.txt_infos = (TextView)findViewById(R.id.txt_infos); this.btn_go_A = (Button)findViewById(R.id.btn_go_A); this.btn_go_A.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent $goto = new Intent( MainActivity.this , B2Activity.class ); startActivityForResult( $goto , MainActivity.this.REQ_GET_RESULT_4_B); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if( requestCode == this.REQ_GET_RESULT_4_B ){ if( resultCode == RESULT_OK ){ String $name = data.getStringExtra("name"); boolean $sex = data.getBooleanExtra( "sex" , false ); this.txt_infos.setText( "姓名 :" + $name + "性別 : " + ($sex ? "男" : "女") ); } } } }
註意 :
1’ final int REQ_GET_RESULT_4_B = 100; 為請求碼(需要自己定義)
2‘ RESULT_OK 結果碼 , 不需要自己定義
② : B
package com.example.kayer.aas; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; public class B2Activity extends AppCompatActivity { private EditText in_name = null; private RadioGroup rg_sex = null; private RadioButton r_male = null; private RadioButton r_female = null; private Button btn_ok = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_b2); this.init(); } private void init(){ this.in_name = (EditText)findViewById(R.id.in_name); this.rg_sex = (RadioGroup)findViewById(R.id.rg_sex); this.r_male = (RadioButton)findViewById(R.id.r_male); this.r_female = (RadioButton)findViewById(R.id.r_female); this.btn_ok = (Button)findViewById(R.id.btn_ok); this.btn_ok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String $name = B2Activity.this.in_name.getText().toString().trim(); if( $name.length() > 0 ){ boolean male = B2Activity.this.r_male.isChecked(); Intent $result = new Intent(); $result.putExtra( "name" , $name ); $result.putExtra( "sex" , male ); B2Activity.this.setResult( RESULT_OK , $result ); B2Activity.this.finish(); }else{ Toast.makeText(B2Activity.this, "請填寫姓名!!!", Toast.LENGTH_SHORT).show(); } } }); this.rg_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { String $sex; if( B2Activity.this.r_male.getId() == checkedId ){ $sex = "男"; }else{ $sex = "女"; } Toast.makeText(B2Activity.this, "您選擇的性別 :" + $sex , Toast.LENGTH_SHORT).show(); } }); } }
Android之Intent(二)