Android 第一个页面TextView里面有一些数据,页面跳转后返回到第一个页面,怎么保存第一个页面TextView里面数据不变。不是为空的。
6条回答 默认 最新
- 怀楠 2018-06-18 04:36关注
.实现效果:
原始界面: ----传输数据----------> 填写数据后,点击计算后界面-----返回数据----->点击返回按钮后,回到上一个页面,依旧能够保留之前保持的数据2.实现代码:
a.两个布局文件:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
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">
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" />
android:layout_marginTop="30dp"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:textSize="20sp"
android:text="@string/sex"
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
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" />
android:layout_marginLeft="30dp"
android:id="@+id/rb2"
android:text="@string/woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_marginTop="10dp"
android:layout_marginLeft="50dp"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:text="@string/height"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:id="@+id/et"
android:layout_marginLeft="10dp"
android:background="@drawable/bg_edittext"
android:layout_width="80dp"
android:layout_height="wrap_content" />
android:layout_marginLeft="10dp"
android:text="cm"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
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" />new_activity.xml文件:
<?xml version="1.0" encoding="utf-8"?>
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" />
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();
}
});
}
}解决 无用评论 打赏 举报
悬赏问题
- ¥15 求差集那个函数有问题,有无佬可以解决
- ¥15 【提问】基于Invest的水源涵养
- ¥20 微信网友居然可以通过vx号找到我绑的手机号
- ¥15 寻一个支付宝扫码远程授权登录的软件助手app
- ¥15 解riccati方程组
- ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
- ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
- ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
- ¥50 树莓派安卓APK系统签名
- ¥65 汇编语言除法溢出问题