无法使用onClick选项卡?

时间:2016-11-14 08:56:01

标签: android

我正在为用户创建设置选项卡,我使用CheckBox进行检查。这是代码:

public void onCheckboxClicked(View view) {
    // Is the view now checked?
    boolean checked = ((CheckBox) view).isChecked();

    // Check which checkbox was clicked
    switch(view.getId()) {
        case R.id.Lang_Vie:
            if (checked){
                mCBLangEng.setChecked(false);
                Constants.STR_LANG = "vi";
                setLocale("vi");
            }
            break;
        case R.id.Lang_Eng:
            if (checked){
                mCBLangViet.setChecked(false);
                Constants.STR_LANG = "en";
                setLocale("en");
            }
        case R.id.locHanoi:
            if (checked){
                mCBLocDN.setChecked(false);
                Constants.STR_LOC = "hn";
                Constants.API_TYPE_AQI = 1;
                Constants.API_TYPE_TMP = 2;
                Constants.API_TYPE_HUM = 3;
            }
        case R.id.locDanang:
            if (checked){
                mCBLocHN.setChecked(false);
                Constants.STR_LOC = "dn";
                Constants.API_TYPE_AQI = 7;
                Constants.API_TYPE_TMP = 8;
                Constants.API_TYPE_HUM = 9;
            }
            break;
        case R.id.SensCheck:
            if (checked){
                Constants.STR_SENS = "yes";
                Constants.USER_LIMITATION = 200;
            }
            else{
                Constants.STR_SENS = "no";
                Constants.USER_LIMITATION = 150;
            }
            break;
    }
}

我在SettingTab中使用此代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.view_setting, container, false);
    Sens = (CheckBox) view.findViewById(R.id.SensCheck);
    mCBLangEng  = (CheckBox) view.findViewById(R.id.Lang_Eng);
    mCBLangViet = (CheckBox) view.findViewById(R.id.Lang_Vie);
    mCBLocHN    = (CheckBox) view.findViewById(R.id.locHanoi);
    mCBLocDN    = (CheckBox) view.findViewById(R.id.locDanang);
    Save        = (Button) view.findViewById(R.id.save);
    onCheckboxClicked(view);
    return view;

但我有这个错误:

11-14 15:50:24.574 16794-16794/com.journaldev.viewpager E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.CheckBox
                                                                          at com.journaldev.viewpager.MyApplication.MyApp.TabSetting.onCheckboxClicked(TabSetting.java:155)
                                                                          at com.journaldev.viewpager.MyApplication.MyApp.TabSetting.onCreateView(TabSetting.java:140)

也许出现了问题?我试图用this.getActivity()替换视图,但它不会工作。

你能帮帮我吗?谢谢!。

这是我的布局:

<?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="fill_parent"
    android:orientation="vertical">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000"
        android:layout_weight="1">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/Setting"
                android:textSize="35dp"
                android:textColor="#faf7f7"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/Language"
                android:textSize="30sp"
                android:textColor="#faf7f7"
                android:layout_marginTop="20dp"/>

            <CheckBox
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="English"
                android:textSize="20sp"
                android:textColor="#faf7f7"
                android:checked="false"
                android:id="@+id/Lang_Eng"
                android:onClick="onCheckboxClicked"/>
            <CheckBox
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="Tiếng Việt"
                android:textSize="20sp"
                android:textColor="#faf7f7"
                android:checked="true"
                android:id="@+id/Lang_Vie"
                android:onClick="onCheckboxClicked"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/Location"
                android:textSize="30sp"
                android:textColor="#faf7f7"
                android:layout_marginTop="20dp"/>

            <CheckBox
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="@string/Danang"
                android:textSize="20sp"
                android:textColor="#faf7f7"
                android:checked="false"
                android:id="@+id/locDanang"
                android:onClick="onCheckboxClicked"/>
            <CheckBox
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="@string/Hanoi"
                android:textSize="20sp"
                android:textColor="#faf7f7"
                android:checked="true"
                android:id="@+id/locHanoi"
                android:onClick="onCheckboxClicked"/>


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/User"
                android:textSize="30sp"
                android:textColor="#faf7f7"
                android:layout_marginTop="20dp"/>
            <CheckBox
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:checked="false"
                android:textSize="20sp"
                android:textColor="#faf7f7"
                android:text="@string/Sensitivegroup"
                android:id="@+id/SensCheck"
                android:onClick="onCheckboxClicked"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:layout_marginTop="10dp"
                android:textStyle="italic"
                android:textColor="#faf7f7"
                android:text="@string/Note"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:layout_marginTop="10dp"
                android:textStyle="italic"
                android:textColor="#faf7f7"
                android:text="@string/Example"/>

        </LinearLayout>
    </ScrollView>
    <Button
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#bfbebe"
        android:text="SAVE SETTING"
        android:textColor="#000000"
        android:id="@+id/save"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您不仅在onCheckboxClicked(view);上致电Layout CheckBox。尝试替换

boolean checked = ((CheckBox) view).isChecked(); 

  if (view instanceof CheckBox) {
    boolean checked = ((CheckBox) view).isChecked(); 
    ...
  }

或者将setOnCheckedChangeListener()添加到CheckBox es

  mCBLangEng.setOnCheckedChangeListener(...);