您好,登录后才能下订单哦!
前一篇(Android之Intent(一))讲解过A(它用来调起B界面,这里先理解为父界面) 可以向B(这里先理解为子界面), 本篇正好是一个逆反的过程 , 即B向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-1‘ (RadioButton)r_male : 男
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();
}
});
}
}
三 : 结果
① , 初始界面A(activity_main.xml)
② , 进入到B (activity_b2.xml), 并填写数据 。
③ ,当B点击“确定”后回到A界面,在A界面显示B传过来的参数
ok -------------
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。