2015-10-14 162 views
0

在我的應用程序中有三個按鈕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); 
    } 
}); 

yes no maybe佈局下面的代碼顯示了當event點擊 此事件列表視圖,當我點擊它應該表現出什麼我已單擊

+0

在按鈕中添加選擇器 – Destro

+0

可以使用breif –

+0

來解釋您必須將狀態保存在數據庫或共享首選項的某個位置,並且當您再次進入活動狀態時,必須將其與視圖綁定。 –

回答

0

使用每個事件單事件像這樣的國家選擇器希望這會幫助你...

create 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> 

border.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> 
0
  1. 如果要求僅用於保存3個按鈕狀態,則可以使用sharedpreference來保存選定的按鈕值。
  2. 如果是選擇保存值爲0,沒有選擇另存爲1,可以點擊保存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(); 
  • ,並獲得在活動的onResume下一次(所保存的值)像這樣

    SharedPreferences sharedpreferences = getSharedPreferences方法( 「偏愛」,Context.MODE_PRIVATE) ;
    int clickedButton = sharedpreferences.getInt(「clicked_btn」,1); // 1是默認值。

  • 添加開關盒,並根據clickedButton值更改上次保存按鈕的按鈕顏色。

  • 開關的情況下應該是這樣的

    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; 
    } 
    
  • 希望這將幫助你。

    相關問題