单击按钮并更改另一个按钮的背景颜色 [英] Click Button and change background color of another button

查看:140
本文介绍了单击按钮并更改另一个按钮的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Android Studio中制作一个应用.在我的一项活动中,有一堆按钮,当您单击一个按钮时,会出现一个PopupWindow类,该类上有4种不同颜色的按钮.

I am making an app in Android Studio. On one of my activities, there are a bunch of buttons, and when you click one, a PopupWindow class appears which has 4 different coloured buttons on it.

我遇到的问题:PopupWindow出现后,我希望用户选择4个按钮之一,并根据他们选择的一种颜色,首先在活动上单击的原始按钮将更改其背景颜色.到在弹出窗口中选择的那个.

What I am having trouble with: Once the PopupWindow appears, I want the user to select one of the 4 buttons and based on the one colour they chose, the original button that was first clicked on the activity will change its background colour to the one selected in the Popup.

弹出代码:

public class Pop extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.popupwindow);

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int width = dm.widthPixels;
    int height = dm.heightPixels;

    getWindow().setLayout((int)(width*.7), (int)(height*.7));

   final Button color1Button = (Button) findViewById(R.id.redbutton);
    color1Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    }
}

主要活动代码:

Button a = (Button) findViewById(R.id.button);

    a.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            startActivity(new Intent(LambertsLaneSection1Activity.this, Pop.class));

        }
    });
    Button b = (Button) findViewById(R.id.button3);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(LambertsLaneSection1Activity.this, Pop.class));


        }
    });
    Button c = (Button) findViewById(R.id.button4);
    c.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(LambertsLaneSection1Activity.this, Pop.class));


        }
    });

推荐答案

如果我正确理解您的目标,则可以使用SharedPreferences和Extras实现此目标的一种可能方法.

If I understand your goal correctly, one possible way to accomplish this is by using SharedPreferences and Extras.

共享首选项"文件是本地存储在设备文件系统中的文件.它使您可以将信息存储为键值对,并且即使退出应用程序,该信息也会保留在那里.它主要用于存储您的应用程序特定的设置.这样,您可以为用户选择的每个按钮永久保存颜色.

A Shared Preferences file is a file stored in the device's file system locally. It allows you to store information as key-value pairs, and the information remains there even when you exit your application. It is mainly used for storing your application-specific settings. This way, you can permanently save the colors for each button selected by the user.

其他用于在使用Intent从另一个活动中启动活动时在Activite之间传输信息.

Extras are for transporting information between Activites when you start an Activity from another using an Intent.

您需要将此添加到 MainActivity onCreate():

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences sharedPref = getSharedPreferences(
                "MY_PREFS", Context.MODE_PRIVATE);

        ...
}

按钮初始化和处理按键的代码(例如 button2 ):

Code for button initializing and handling press (e.g button2):

//-1 is a default value in case no color was selected yet for the particular button
int button2Color = sharedPref.getInt("button2Color", -1);
Button button2 = (Button) findViewById(R.id.button2);
if (button2Color != -1) {
      button2.setBackgroundColor(button2Color);
}
button2.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
           Intent i = new Intent(LambertsLaneSection1Activity.this, Pop.class);
           //2 here identifies your button, because it is button2
           i.putExtra("buttonPressed",2)
           startActivity(i);
     }
});

在您的弹出窗口中:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.popupwindow);

    //here you get which button was pressed as an Extra
    int buttonPressed = getIntent().getExtras().getInt("buttonPressed");
    SharedPreferences sharedPref = getSharedPreferences(
                "MY_PREFS", Context.MODE_PRIVATE);
    ...

    final Button color1Button = (Button) findViewById(R.id.redbutton);
    color1Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             sharedPref.edit().putInt("button"+buttonPressed+"Color", Color.RED).apply();
        }
    }
}

这篇关于单击按钮并更改另一个按钮的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆