I would like to do many button in one page,and that page when user use the android phone to click one of the button in that page, other all button will disabled and only the button clicked will become red and when reopen the application, the button clicked still is red. If who know how to do, please tell me. Below is my coding:
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button" />
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button2" />
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button3" />
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button4" />
This is my main activity xml.
public class MainActivity extends AppCompatActivity {
Button button,button1,button2,button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button1 = (Button) findViewById(R.id.button2);
button2 = (Button) findViewById(R.id.button3);
button3 = (Button) findViewById(R.id.button4);
if(button.isClickable()) {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button.setEnabled(false);
button.setBackgroundColor(Color.RED);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
}
});
}
if(button1.isClickable()) {
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button.setEnabled(false);
button1.setBackgroundColor(Color.RED);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
}
});
}
if(button2.isClickable()) {
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button.setEnabled(false);
button2.setBackgroundColor(Color.RED);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
}
});
}
if(button3.isClickable()) {
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button.setEnabled(false);
button3.setBackgroundColor(Color.RED);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
}
});
}
Log.e("onCreate: ", "UniqueKey: " + getUniqueKey());
}
public String getUniqueKey(){
String android_id = Settings.Secure.getString(this.getContentResolver(),
Settings.Secure.ANDROID_ID);
return android_id;
}
} This is my main activity java. This is every android phone can click the button one time, after click and reopen the application, the button will disabled and red and also other button will disable ,thanks
将按钮的状态保存在SharedPreferences中 ,然后再次打开应用程序时,检查首选项中的值并加载布局。
There is an option called SharedPreferences in Android which allows you to save the instance of any state of your App to the memory and then to retrieve it from the settings. So just save the button ID to the SharedPreferences and when you open the application get the ID of the button in the preference and then disable the other buttons you need i be disabled.
Shared Preference Example
1.store value in sharedPrefernces
:
SharedPreferences preferences = this.getSharedPreferences("SoldiPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("storevalue", false);
editor.commit();
2.get value from sharedPreferences
: In Oncreate of your Activity
SharedPreferences preferences = this.getSharedPreferences("SoldiPreferences", Context.MODE_PRIVATE);
preferences.getBoolean("storevalue", false);
if(preferences.getBoolean("storevalue", false))
{
button.setclickable(true);
button.setenable(true);
}
else{
button.setclickable(false);
button.setenable(false);
}
onButtonCLikListner update sharefpreffernce value
final Button button=new Button(this); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {
SharedPreferences preferences = getApplicationContext().getSharedPreferences("SoldiPreferences", Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); if(preferences.getBoolean("storevalue", false)) { editor.putBoolean("storevalue", false); editor.commit(); } else{ editor.putBoolean("storevalue", true); editor.commit(); } button.setclickable(true); button.setenable(true); } });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.