我已经尝试过这些代码:
radioButton.setEnabled(false);
radioButton.setClickable(false);
但它对我不起作用,因为当我尝试再次使用单选按钮返回活动时,它会再次点击。
我想要的是,当我点击一次时,即使我尝试回到活动,它也不会再次点击。
这些是我活动的代码
public class TeacherSideQuestion extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.question1);
Button btnNext = (Button) findViewById(R.id.btnNext);
Button btnCheck = (Button) findViewById(R.id.btnCheckTeacher1);
btnNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(),
TeacherSideQuestion2.class);
startActivityForResult(intent, 0);
}
});
btnCheck.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences my_preferences = PreferenceManager
.getDefaultSharedPreferences(getApplication());
SharedPreferences.Editor editor = my_preferences.edit();
RadioButton rbtnTeacher1_1 = (RadioButton) findViewById(R.id.rbtnTeacher1_1);
RadioButton rbtnTeacher1_2 = (RadioButton) findViewById(R.id.rbtnTeacher1_2);
RadioButton rbtnTeacher1_3 = (RadioButton) findViewById(R.id.rbtnTeacher1_3);
if (rbtnTeacher1_1.isChecked()) {
Toast.makeText(TeacherSideQuestion.this, "Correct",
Toast.LENGTH_SHORT).show();
rbtnTeacher1_1.setClickable(false);
rbtnTeacher1_2.setClickable(false);
rbtnTeacher1_3.setClickable(false);
if (rbtnTeacher1_1.isChecked()) {
editor.putInt("key1", 1);
} else {
editor.putInt("key1", 0);
}
editor.commit();
} else if (rbtnTeacher1_2.isChecked()) {
Toast.makeText(TeacherSideQuestion.this, "Incorrect",
Toast.LENGTH_SHORT).show();
rbtnTeacher1_1.setTextColor(Color.RED);
rbtnTeacher1_1.setClickable(false);
rbtnTeacher1_2.setClickable(false);
rbtnTeacher1_3.setClickable(false);
if (rbtnTeacher1_2.isChecked()) {
editor.putInt("key1", 0);
}
editor.commit();
} else if (rbtnTeacher1_3.isChecked()) {
Toast.makeText(TeacherSideQuestion.this, "Incorrect",
Toast.LENGTH_SHORT).show();
rbtnTeacher1_1.setTextColor(Color.RED);
rbtnTeacher1_1.setClickable(false);
rbtnTeacher1_2.setClickable(false);
rbtnTeacher1_3.setClickable(false);
if (rbtnTeacher1_3.isChecked()) {
editor.putInt("key1", 0);
}
editor.commit();
} else {
return;
}
}
});
}
}
答案 0 :(得分:1)
您需要检查sharedpreferences
中的值是什么?根据您需要设置的值
radioButton.setEnabled(false).
发布您的代码,我会向您解释如何处理此问题。
修改:
根据您的代码,您需要在用户单击“提交”按钮时检查已检查状态。
你可以这样做。
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.temp);
SharedPreferences _mypref = getApplicationContext()
.getSharedPreferences("mypref", 0); // <--- 0 - for private
Editor editor = _mypref.edit();
first = _mypref.getString("first", "");
second = _mypref.getString("second", "");
Log.e("=first==>", "===>" + first);
Log.e("=second==>", "===>" + second);
rbtnTeacher1_1 = (RadioButton) findViewById(R.id.radioMale);
rbtnTeacher1_2 = (RadioButton) findViewById(R.id.radioFemale);
if (first.length() >= 0) {
if (first.equals("true")) {
rbtnTeacher1_1.setChecked(true);
} else {
rbtnTeacher1_1.setChecked(false);
}
}
if (second.length() >= 0) {
if (second.equals("true")) {
rbtnTeacher1_2.setChecked(true);
} else {
rbtnTeacher1_2.setChecked(false);
}
}
}
public void onCheck(View v) {
if (rbtnTeacher1_1.isChecked() == true) {
SharedPreferences _mypref = getApplicationContext()
.getSharedPreferences("mypref", 0); // <--- 0 - for private
Editor editor = _mypref.edit();
editor.putString("first", "true");
editor.commit();
} else {
SharedPreferences _mypref = getApplicationContext()
.getSharedPreferences("mypref", 0); // <--- 0 - for private
Editor editor = _mypref.edit();
editor.putString("first", "false");
editor.commit();
}
if (rbtnTeacher1_2.isChecked() == true) {
SharedPreferences _mypref = getApplicationContext()
.getSharedPreferences("mypref", 0); // <--- 0 - for private
Editor editor = _mypref.edit();
editor.putString("second", "true");
editor.commit();
} else {
SharedPreferences _mypref = getApplicationContext()
.getSharedPreferences("mypref", 0); // <--- 0 - for private
Editor editor = _mypref.edit();
editor.putString("second", "false");
editor.commit();
}
}
你的xml看起来像......
<?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:baselineAligned="true"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="male" />
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="female" />
</RadioGroup>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onCheck"
android:text="Check" />
</LinearLayout>
选择任何一个收音机,点击复选按钮。现在,当您再次参加活动时,您可以看到所选的单选按钮。
感谢。
答案 1 :(得分:0)
在代码下面的oncreate检查。
SharedPreferences my_preferences = PreferenceManager
.getDefaultSharedPreferences(getApplication());
int i = my_preferences.getInt("key1", 0);
if(i == 1)
// set clickable false