【问题标题】:Android checkbox checked in one activity and then text appears in another activity that notifies that checkbox has been checkedAndroid复选框在一个活动中选中,然后文本出现在另一个活动中,通知该复选框已被选中
【发布时间】:2012-01-07 05:20:17
【问题描述】:

所以我有一个带有复选框和按钮的活动以及另一个带有文本视图的活动。 当我选中复选框并单击按钮时,按钮会调用 startActivty(intent),它会使用 textview 启动其他活动,并应通过文本通知用户复选框已被选中。我该怎么做?

【问题讨论】:

标签: android


【解决方案1】:

在开始另一个活动之前,通过复选框检查并不像捆绑中的外部假设 1 被选中。

Intent i = new Intent(this, FindAndroidActivity.class);
i.putExtra("Checkbox","1");

在您的其他活动中,获取该值

Bundle extras = getIntent().getExtras();
if(extras !=null) {
    String value = extras.getString("Checkbox");
 //Check if value is '1', if so, it is checked and add whatever text you want to textbox here.
}

【讨论】:

  • 只是对上面的补充:如果 CheckBox 的文本是静态的,你可以只传递一个布尔值来简化逻辑。
【解决方案2】:
Intent intent=new Intent();
intent.putExtra("Checked",true);

然后以此意图开始您的活动。在第二个活动中覆盖 Activity .onStart()

@Override
public void onStart(){
    super.onStart();

    Bundle bundle=getIntent.getExtras();
    if(bundle!=null){
        if(bundle.getBoolean("Checked")) /*do your stuff*/
    }
}

【讨论】:

  • 感谢这工作!只有一件事。当我使用 textview 离开活动并返回到它时,我设置的文本不再存在。
  • 这是另一个问题。阅读有关保存活动状态的信息。
猜你喜欢
  • 2016-12-20
  • 2016-12-20
  • 2023-03-13
  • 1970-01-01
  • 2013-12-06
  • 2021-08-23
  • 2014-03-07
  • 1970-01-01
  • 2020-09-30
相关资源
最近更新 更多