【问题标题】:Click Button and change background color of another button单击按钮并更改另一个按钮的背景颜色
【发布时间】:2016-03-31 04:52:28
【问题描述】:

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

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

弹出代码:

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


        }
    });

【问题讨论】:

    标签: java android class button android-studio


    【解决方案1】:

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

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

    Extras 用于在您使用 Intent 从另一个 Activity 启动 Activity 时在 Activity 之间传输信息。

    您需要将此添加到您的MainActivityonCreate()

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            SharedPreferences sharedPref = getSharedPreferences(
                    "MY_PREFS", Context.MODE_PRIVATE);
    
            ...
    }
    

    按钮初始化和处理按下的代码(例如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();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-26
      • 1970-01-01
      • 1970-01-01
      • 2014-10-09
      • 2017-08-02
      • 1970-01-01
      • 2013-06-02
      • 1970-01-01
      • 2014-10-10
      相关资源
      最近更新 更多