【问题标题】:checkbox filter in androidandroid中的复选框过滤器
【发布时间】:2015-07-22 16:46:55
【问题描述】:

我正在尝试了解有关 android 中复选框功能的更多信息。我想在 TextView 中显示选中复选框的值。在我的代码中,它显示了所有复选框,无论是 true(如果选中)还是 false(如果未选中),但我只想打印那些选中的复选框并排除那些未选中的复选框。我尝试使用“如果,否则如果”,但它不起作用。任何帮助将不胜感激。

主活动:

public class Demo extends Activity {

    private CheckBox linux, macos, windows;
    private Button button;
    private EditText ed1, ed2;
    private TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo);
        addListenerOnButton();
    }

    public void addListenerOnButton() {

        linux = (CheckBox) findViewById(R.id.checkBox1);
        macos = (CheckBox) findViewById(R.id.checkBox2);
        windows = (CheckBox) findViewById(R.id.checkBox3);
        ed1 = (EditText) findViewById(R.id.editText1);
        ed2 = (EditText) findViewById(R.id.editText2);
        button = (Button) findViewById(R.id.go);
        text = (TextView) findViewById(R.id.textView1);

        button.setOnClickListener(new OnClickListener() {

              @Override
              public void onClick(View v) {
                StringBuffer result = new StringBuffer();
                result.append("Android check : ").append(linux.isChecked());
                result.append("\nWindows OS check : ").append(macos.isChecked());
                result.append("\niOS check :").append(windows.isChecked());


                //Toast.makeText(Demo.this, result.toString(), Toast.LENGTH_LONG).show();
                text.setText(result);
              }
        });
    }
}

【问题讨论】:

    标签: android checkbox


    【解决方案1】:

    你可以这样做:

            @Override
            public void onClick(View v) {
    
                StringBuffer result = new StringBuffer();
                if (linux.isChecked()) {
                    result.append("Android check : ").append(linux.isChecked());
                }
                if (macos.isChecked()) {
                    result.append("Android check : ").append(linux.isChecked());
                }
                if (windows.isChecked()) {
                    result.append("\niOS check :").append(windows.isChecked());
                }
                text.setText(result);
            }
        });
    }
    

    【讨论】:

      【解决方案2】:

      您的布局 xml 中是否有可能做错了什么? 我复制粘贴了您的代码并使用了以下布局,一切正常:

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
      
      <CheckBox
          android:id="@+id/checkBox1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="New CheckBox"/>
      
      <CheckBox
          android:id="@+id/checkBox2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="New CheckBox"/>
      
      <CheckBox
          android:id="@+id/checkBox3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="New CheckBox"/>
      
      <Button
          android:id="@+id/go"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="New Button"/>
      
      <TextView
          android:id="@+id/textView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="New Text"/>
      
      </LinearLayout>
      

      【讨论】:

      • @Erfan Gholampour 很棒。非常感谢。这正是我想要的。
      猜你喜欢
      • 1970-01-01
      • 2021-07-18
      • 2014-07-21
      • 2011-10-25
      • 1970-01-01
      • 2018-01-27
      • 2019-02-12
      • 2015-08-05
      • 1970-01-01
      相关资源
      最近更新 更多