在android studio

时间:2017-03-29 10:25:26

标签: android-studio checkbox

我在Android Studio工作,我有四个用户问题的复选框。但是,只能选择两个。选中2后,其余复选框应禁用。

package com.example.android.tester;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;

public class MainActivity extends Activity {

    private CheckBox chkIos, chkAndroid, chkWindows;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addListenerOnChkIos();
        addListenerOnChkAndroid();
        addListenerOnChkWindows();
    }

    int count = 0;
    public void addListenerOnChkIos() {

        chkIos = (CheckBox) findViewById(R.id.chkIos);

        chkIos.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //is chkIos checked?
               if (count == 2) {
                    chkIos.setEnabled(false);
                } else
                    chkIos.setEnabled(true);
                    count++;

            }
        });

    }

    public void addListenerOnChkAndroid() {

    chkAndroid = (CheckBox) findViewById(R.id.chkAndroid);

    chkAndroid.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //is chkAndroid checked?
            if (count == 2) {
                chkAndroid.setEnabled(false);
            } else
                chkAndroid.setEnabled(true);
            count++;

        }
    });

}

public void addListenerOnChkWindows() {

    chkWindows = (CheckBox) findViewById(R.id.chkWindows);

    chkWindows.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //is chkWindows checked?
            if (count == 2) {
                chkWindows.setEnabled(false);
            } else
                chkWindows.setEnabled(true);
            count++;

        }
    });

}
}

这段代码主要来自我一直在试验的Mkyong教程。如果我运行它,它允许检查所有三个,然后只有在我取消选中一个之后,它是否禁用。任何帮助表示赞赏。

如果有人想要试用,那么这是xml和strings文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<CheckBox
    android:id="@+id/chkIos"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/chk_ios" />

<CheckBox
    android:id="@+id/chkAndroid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/chk_android"
    android:checked="true" />

<CheckBox
    android:id="@+id/chkWindows"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/chk_windows" />

<Button
    android:id="@+id/btnDisplay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_display" />

</LinearLayout>

的字符串:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MyAndroidAppActivity!</string>
<string name="app_name">MyAndroidApp</string>
<string name="chk_ios">IPhone</string>
<string name="chk_android">Android</string>
<string name="chk_windows">Windows Mobile</string>
<string name="btn_display">Display</string>
</resources>

0 个答案:

没有答案