【问题标题】:how to hold a changed colour of the button which clicked by user如何保持用户单击的按钮的更改颜色
【发布时间】:2016-01-12 07:36:30
【问题描述】:

在我的应用程序中有三个按钮yes no maybe。在这里,当用户单击颜色更改的任何按钮时,我更改了按钮的颜色。当用户单击按钮时,它根据用户选择按钮在表中存储为 1 0 2。但是当我在应用程序中选择其他选项并返回到此按钮时,按钮正常。此处应突出显示由用户选择的内容。

yes no maybe button的以下代码

yesBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        // highlight the button when clicked
        yesBtn.setBackgroundColor(Color.YELLOW);
        noBtn.setBackgroundColor(Color.BLUE);
        maybeBtn.setBackgroundColor(Color.BLUE);
        responseLayout.setVisibility(View.GONE);
        //If user clicks yes button in invitation response layout,response would be stored as 1 for event user
        final int response = 1;
        final long eventId = eventMOs.get(position).getEventId();
        userMO.setIsAttending(response);

        new AsyncTask<Void, Void, String>() {
            protected String doInBackground(Void... arg0) {
                return userDelegate.updateEventUserRelationShipMapping(userMO, eventId);
            }
        }.execute(null, null, null);
    }
});
noBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        yesBtn.setBackgroundColor(Color.BLUE);
        noBtn.setBackgroundColor(Color.YELLOW);
        maybeBtn.setBackgroundColor(Color.BLUE);
        responseLayout.setVisibility(View.GONE);
        //If user clicks no button in invitation response layout,response would be stored as 0 for event user
        final int response = 0;
        final long eventId = eventMOs.get(position).getEventId();
        userMO.setIsAttending(response);

        new AsyncTask<Void, Void, String>() {
            protected String doInBackground(Void... arg0) {
                return userDelegate.updateEventUserRelationShipMapping(userMO, eventId);
            }
        }.execute(null, null, null);
    }
});
maybeBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        yesBtn.setBackgroundColor(Color.BLUE);
        noBtn.setBackgroundColor(Color.BLUE);
        maybeBtn.setBackgroundColor(Color.YELLOW);
        responseLayout.setVisibility(View.GONE);
        //If user clicks maybe button in invitation response layout,response would be stored as  for event user
        final int response = 2;
        userMO.setIsAttending(response);
        final long eventId = eventMOs.get(position).getEventId();

        new AsyncTask<Void, Void, String>() {
            protected String doInBackground(Void... arg0) {
                return userDelegate.updateEventUserRelationShipMapping(userMO, eventId);
            }
        }.execute(null, null, null);
    }
});

event 被点击时,这个yes no maybe 布局会显示 这里的事件是列表视图,当我单击单个事件时,它应该为每个事件显示我单击的内容

【问题讨论】:

  • 在按钮中添加选择器
  • 你能解释一下吗
  • 您必须将状态保存在数据库或共享首选项等位置,当您再次进入活动时,您必须将其与视图绑定。

标签: java android


【解决方案1】:

在这样的状态下使用选择器希望这会对你有所帮助...

创建 btn_stae.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_shortAnimTime">

    <item android:drawable="@drawable/hidden_bg" android:state_focused="true" android:state_pressed="false"/>
    <item android:drawable="@drawable/bg_round" android:state_pressed="true"/>
    <item android:drawable="@drawable/border" android:state_enabled="false"/>
    <item android:drawable="@drawable/active" />
</selector>

active.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="4dp" />
    <stroke
        android:width="2dp"
        android:color="@color/green_dark" />

    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />

    <solid android:color="@color/white" />


</shape>

边框.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="4dp" />
    <stroke
        android:width="2dp"
        android:color="@color/button_bg_stroke"/>
    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />

    <solid android:color="@color/button_bg" />


</shape>

bg_round.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="@color/switch_thumb_normal_material_dark" />

    <stroke
        android:width="2dp"
        android:color="@color/background_material_light" />
    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />

    <corners android:radius="4dp" />

</shape>

hidden_​​bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="@color/bg" />

    <stroke
        android:width="2dp"
        android:color="@color/light" />

    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />

    <corners android:radius="4dp" />


</shape>

【讨论】:

    【解决方案2】:
    1. 如果要求只是保存 3 个按钮的状态,您可以使用 sharedpreference 来保存选定的按钮值。
    2. 如果选择yes保存值为0,No选择save as 1,可以点击save 2。

    在 sharedpreference 中保存值的代码。在您的按钮单击侦听器上编写类似的代码。

       SharedPreferences sharedpreferences = getSharedPreferences("PREFERENCE", Context.MODE_PRIVATE);   
           int clickedButton = sharedpreferences.getInt("clicked_btn", 1); //1 is the default value.
    SharedPreferences sharedpreferences = getSharedPreferences("PREFERENCE", Context.MODE_PRIVATE); 
    Editor editor = sharedpreferences.edit();
    editor.putInt("clicked_btn", 0); //value will be 0 or 1 or 2
    editor.commit();
    
    1. 并在下次像这样的活动 onResume() 方法中获取保存的值

      SharedPreferences sharedpreferences = getSharedPreferences("PREFERENCE", Context.MODE_PRIVATE);
      int clickedButton = sharedpreferences.getInt("clicked_btn", 1); //1 是默认值。

    2. 添加开关盒并根据 clickedButton 值更改上次保存按钮的按钮颜色。

    3. 开关盒应该是这样的

      switch (clickedButton)
      {
      case 0:  //here change yes button color
          break;
      case 1:  //here change no button color
          break;
      case 2:  //here change may be button color
          break;           
      default: //here default button color for saved button id
          break;
      }
      

    希望这对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-02
      • 2021-10-18
      • 2012-03-29
      • 2021-07-21
      • 2019-05-20
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      相关资源
      最近更新 更多