2015-07-22 4 views
0

Я пытаюсь узнать больше о функциях checkbox в android. Я хочу показать значения проверенных флажков в TextView. В моем коде он показывает все флажки либо true (если отмечено), либо false (если не отмечен), но я хочу только напечатать те флажки, которые отмечены флажками, и исключить их. Я пробовал использовать «if, else if», но не работал. Любая помощь будет действительно оценена.checkbox filter in android

MainActivity:

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); 
       } 
     }); 
    } 
} 

ответ

2

Вы можете сделать это следующим образом:

 @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); 
     } 
    }); 
} 
0

Возможно ли, что вы делаете что-то неправильно в макете 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> 
+0

@Erfan Gholampour Его большой. Большое спасибо. Это именно то, что я искал. –

+0

Без проблем мой друг goodluck –