I'm working in Android Studio and I have four checkboxes for a user question. However, only two can be selected. After 2 are checked, the remaining checkboxes should disable.
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++;
}
});
}
}
This is code mostly from a Mkyong tutorial that I've been experimenting with. If I run this it allows all three to be checked and then only after I uncheck one, does it disable. Any help is appreciated.
Here's the xml and strings file if anyone wants to try it out
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
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>
Strings:
<?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>
Aucun commentaire:
Enregistrer un commentaire