gpt4 book ai didi

android - 如何使用 sharedpreferences 禁用单选按钮或单选组?

转载 作者:行者123 更新时间:2023-11-29 21:21:37 25 4
gpt4 key购买 nike

我已经尝试过这些代码集:

radioButton.setEnabled(false);
radioButton.setClickable(false);

但这对我不起作用,因为当我尝试再次返回到带有单选按钮的 Activity 时,它将再次可点击。

我想要的是,当我点击它一次时,即使我试图返回 Activity ,它也不会再被点击。

这些是我的 Activity 代码

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;
}
}
});
}
}

最佳答案

您需要检查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>

选择单选其中任何一个,单击检查按钮。现在,当您再次进入您的 Activity 时,您可以看到您选中的选定单选按钮。

谢谢。

关于android - 如何使用 sharedpreferences 禁用单选按钮或单选组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20657381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com