android控件checkBox的選中效果

一如既往的先聲明:

    private TextView result; 
    private String china;  

接下來:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        result= (TextView) this.findViewById(R.id.result);//獲取TextView

        CheckBox checkBox= (CheckBox) this.findViewById(R.id.checkBox);//獲取CheckBox 

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {//選中事件
                if(isChecked)
                {
                    china= (String) buttonView.getText();
                }else
                    china="";
                result.setText("你最喜歡的美食是:"+china);
            }
        });
 }

以下是xml的代碼:

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你最喜歡喫的菜是什麼"
        android:id="@+id/textView" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中餐"
        android:id="@+id/checkBox" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你最喜歡的美食是:"
        android:id="@+id/result" />

在這裏我只寫了一個checkBox,可以寫多幾個可以更好體現checkBox控件效果。

如果有錯,歡迎評論指正,學習至上!

更多文章:http://blog.csdn.net/qq_26849491

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章