【问题标题】:CheckBox is checked send item to defferent activity [closed]CheckBox 被选中将项目发送到不同的活动 [关闭]
【发布时间】:2014-02-17 22:03:47
【问题描述】:

在我的应用程序中,当 CheckBox 被选中时,我希望 ListView 中的项目转到另一个 Activity 中的另一个 ListView。请帮助我或给我一些有关如何执行此操作的指示。感谢您的帮助。

public class Check extends Activity{

CheckBox check;
TextView tvItem;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    check = (CheckBox) findViewById(R.id.checkBox1);
    tvItem = (TextView) findViewById(R.id.tvItem);
    check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){

            }else{

            }

        }
    });
}



}

我应该在 if 和 else 中输入什么代码。

【问题讨论】:

  • 不看你的代码就很难帮你。

标签: android android-listview android-activity android-checkbox


【解决方案1】:

基本上,您需要为 CheckBox 设置一个 onClickedListener,并启动 Activity,并传递足够的信息来启动具有正确设置 ListView 的新 Activity。您可能需要切换选中框的状态,以防万一我已经包含了代码,如果看起来有问题,请删除它。

checkBox.setOnClickListener(new OnClickListener() {
    @Override
    void onClick(View v) {
        checkedBox.toggle(); //Remove if check box is toggled correctly

        Intent intent=new Intent(MainActivity.this,OtherActivity.class);
        intent.putExtra("data",someData);
        startActivity(intent);
    }
}

其他活动检查意图,如下所示:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    //Magic happens here.
}

如需了解更多详情,请仔细阅读这篇Android Document 文章。

【讨论】:

    【解决方案2】:

    您可以捆绑信息(例如项目的名称)并通过 Intent 将其发送到另一个活动,无论是在检查时还是通过按钮单击。

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_menu);
    
            final String MSTag = "MSTag"; //declare this string as your tag
                String itemName = YOUR_LIST_ITEM_NAME
    
    ...
           Intent myIntent = new Intent(this, YOUR_NEW_ACTIVITY.class);  
           myIntent.putExtra(MSTag, itemName); // add your tag and the item name to your intent.
    
           startActivity(m1Intent);
    

    然后,在你的新活动中:

    Bundle intent = getIntent().getExtras(); // this gets your added extras from the previous activity
    myTag = intent.getString("MSTag"); // matches on your tag, and gets the string value
    String itemName = myTag;
    ...
    

    然后将您的项目添加到您的新 Activity 中的 ListView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-02
      相关资源
      最近更新 更多