gpt4 book ai didi

java - 第一个按钮仅在重新打开应用程序后才着色

转载 作者:行者123 更新时间:2023-12-01 17:55:34 25 4
gpt4 key购买 nike

我已经在堆栈溢出中多次问过这个问题,我希望这次有人能告诉我正确的代码,我修改了我的编码很多次仍然是相同的输出。编码:

public class MainActivity extends AppCompatActivity {

Toolbar mToolbar;
Button mRedColor;
Button mGreenColor;
Button mYellowColor;
Button[] b=new Button[2];
SharedPreferences mSharedPreferences;
SharedPreferences.Editor edit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSharedPreferences = getSharedPreferences("ButtonColor", MODE_PRIVATE);
edit = getSharedPreferences("ButtonColor", MODE_PRIVATE).edit();
mToolbar = (Toolbar) findViewById(R.id.toolbar);
final Button[] b = new Button[]{(Button) findViewById(R.id.btnRed),
(Button) findViewById(R.id.btnGreen),
(Button) findViewById(R.id.btnYellow),};

mToolbar.setTitle(getResources().getString(R.string.app_name));

if (getColor() != getResources().getColor(R.color.colorPrimary)) {
for (int i = 0 ; i<b.length; i++){

if(b[i].equals(b[0]) ){

b[0].setBackgroundColor(getColor());
b[i].setEnabled(false);

}
else if (b[i].equals(b[1])){

b[1].setBackgroundColor(getColor());
b[i].setEnabled(false);
}else if (b[i].equals(b[2])){

b[2].setBackgroundColor(getColor());
b[i].setEnabled(false);
}

}
}

for (int i = 0; i < b.length; i++) {

b[i].setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
switch (view.getId()) //so we get its id here
{
case (R.id.btnRed):
b[0].setBackgroundColor(getResources().getColor(R.color.colorRed));
storeColor(getResources().getColor(R.color.colorRed));
b[0].setEnabled(false);

break;
case (R.id.btnGreen):
b[1].setBackgroundColor(getResources().getColor(R.color.colorGreen));
storeColor(getResources().getColor(R.color.colorGreen));
b[1].setEnabled(false);
break;
case (R.id.btnYellow):
b[2].setBackgroundColor(getResources().getColor(R.color.colorYellow));
storeColor(getResources().getColor(R.color.colorYellow));
b[2].setEnabled(false);
break;
}
}
});
}
}
@Override
protected void onResume() {
super.onResume();
mSharedPreferences = getSharedPreferences("ButtonColor", MODE_PRIVATE);
edit=getSharedPreferences("ButtonColor", MODE_PRIVATE).edit();
}

@Override
public void onStop () {
super.onStop();
}
private void storeColor(int color){
SharedPreferences mSharedPreferences =
getSharedPreferences("ButtonColor", MODE_PRIVATE);
SharedPreferences.Editor mEditor = mSharedPreferences.edit();
mEditor.putInt("color", color);
mEditor.apply();
}
private int getColor(){
SharedPreferences mSharedPreferences =
getSharedPreferences("ButtonColor", MODE_PRIVATE);
int selectedColor = mSharedPreferences.getInt("color",
getResources().getColor(R.color.colorPrimary));
return selectedColor;
}
}

我面临的问题在这里:The first Button only will be coloured permanently after reopen application ....,谢谢

最佳答案

尝试下面的代码。您将能够更改所有按钮的颜色,但它只会存储最后选择的按钮颜色。

       public class MainActivity extends AppCompatActivity {
Toolbar mToolbar;
Button mRedColor;
Button mGreenColor;
Button mYellowColor;
Button[] b = new Button[2];
SharedPreferences mSharedPreferences;
SharedPreferences.Editor edit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color);
mSharedPreferences = getSharedPreferences("ButtonColor", MODE_PRIVATE);
edit = getSharedPreferences("ButtonColor", MODE_PRIVATE).edit();
mToolbar = (Toolbar) findViewById(R.id.toolbar);
final Button[] b = new Button[]{(Button) findViewById(R.id.btnRed),
(Button) findViewById(R.id.btnGreen),
(Button) findViewById(R.id.btnYellow),};

mToolbar.setTitle(getResources().getString(R.string.app_name));
int lastPostion = getButtonPosition();
if (getColor() != getResources().getColor(R.color.colorPrimary)) {
for (int i = 0; i < b.length; i++) {

if (b[i].equals(b[0]) && i == lastPostion) {

b[0].setBackgroundColor(getColor());
b[i].setEnabled(false);

} else if (b[i].equals(b[1]) && i == lastPostion) {

b[1].setBackgroundColor(getColor());
b[i].setEnabled(false);

} else if (b[i].equals(b[2]) && i == lastPostion) {

b[2].setBackgroundColor(getColor());
b[i].setEnabled(false);


}

}
}

for (int i = 0; i < b.length; i++) {

b[i].setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
switch (view.getId()) //so we get its id here
{
case (R.id.btnRed):
b[0].setBackgroundColor(getResources().getColor(android.R.color.holo_red_dark));
storeColor(getResources().getColor(android.R.color.holo_red_dark), 0);
b[0].setEnabled(false);

break;
case (R.id.btnGreen):
b[1].setBackgroundColor(getResources().getColor(android.R.color.holo_green_dark));
storeColor(getResources().getColor(android.R.color.holo_green_dark), 1);
b[1].setEnabled(false);
break;
case (R.id.btnYellow):
b[2].setBackgroundColor(getResources().getColor(android.R.color.black));
storeColor(getResources().getColor(android.R.color.black), 2);
b[2].setEnabled(false);
break;
}
}
});
}
}

@Override
protected void onResume() {
super.onResume();
mSharedPreferences = getSharedPreferences("ButtonColor", MODE_PRIVATE);
edit = getSharedPreferences("ButtonColor", MODE_PRIVATE).edit();
}

@Override
public void onStop() {
super.onStop();
}

private void storeColor(int color, int position) {
SharedPreferences mSharedPreferences =
getSharedPreferences("ButtonColor", MODE_PRIVATE);
SharedPreferences.Editor mEditor = mSharedPreferences.edit();
mEditor.putInt("color", color);
mEditor.putInt("position", position);
mEditor.apply();
}

private int getColor() {
SharedPreferences mSharedPreferences =
getSharedPreferences("ButtonColor", MODE_PRIVATE);
int selectedColor = mSharedPreferences.getInt("color",
getResources().getColor(R.color.colorPrimary));
return selectedColor;
}

private int getButtonPosition() {
SharedPreferences mSharedPreferences =
getSharedPreferences("ButtonColor", MODE_PRIVATE);
int selectedColor = mSharedPreferences.getInt("position", 0);
return selectedColor;
}

}

关于java - 第一个按钮仅在重新打开应用程序后才着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45204672/

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