私は1ページで多くのボタンを使いたいと思います。そのページでボタンの1つをクリックするためにアンドロイド電話を使用するとそのページ、クリックされたボタンだけが赤色になり、アプリケーションを再度開くと、クリックされたボタンはまだ赤色になります。どうすればいいのか分かっている人は、教えてください。以下は私のコーディングです:再度ボタンをクリックする方法は、アプリケーションを再度開くと無効になります
<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" />
これは私の主な活動の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;
}
} これは私の主な活動のJavaです。これは、すべてのAndroid携帯電話は、ボタンは無効と赤と、他のボタンは無効になりますクリックした後、ボタンを1回クリックし、アプリケーションを再度開くことができSharedPreferencesでボタンの状態保存のおかげ
https://stackoverflow.com/questions/44962705/how-to-hide -views-for-always/44962743#44962743これはあなたを助けるでしょう –
あなたはクリック可能にするために使用することができますvisiblityのリンクストアの状態 –
私にコードを表示することができます、私は保存する方法を理解していません –