2016-04-18 74 views
0

你好我正在我的Android項目。我創建了一個使用四個按鈕的活動,我希望用戶選擇任何一個。我通過將其他按鈕設置爲.setClickable(false)來完成此操作。但是當我重新啓動或重新啓動應用程序時,所有按鈕都會啓用。我希望當用戶選擇一個按鈕時,它應該保存它的選擇,以便在app的retstart上,用戶不會感到困惑。下面是一段代碼:Android:保存按鈕實例

Notifications.class

  package com.mateoj.multiactivitydrawer; 

import android.app.Notification; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.support.v7.app.NotificationCompat; 
import android.view.MenuItem; 
import android.view.View; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import com.mateoj.multiactivitydrawer.department.dept_1styeartab; 
import com.pushbots.push.Pushbots; 

public class notifications extends BaseActivity { 
    private WebView webView; 

    Button clickButton; 
    Button clickButton1; 
    Button clickButton2; 
    Button clickButton3; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Pushbots.sharedInstance().init(this); 
     Pushbots.sharedInstance().setCustomHandler(customHandler.class); 
     setContentView(R.layout.activity_notifications); 
     final SharedPreferences preferences = getSharedPreferences("com.mateoj.multiactivitydrawer", MODE_PRIVATE); 
     final SharedPreferences.Editor editor = preferences.edit(); 

     clickButton = (Button) findViewById(R.id.ncs); 
     clickButton1 = (Button) findViewById(R.id.nmech); 
     clickButton2 = (Button) findViewById(R.id.nece); 
     clickButton3 = (Button) findViewById(R.id.neee); 



     clickButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 


       Intent show = new Intent(getApplicationContext(), noti_cse.class); 
       Pushbots.sharedInstance().tag("cse"); 
       Pushbots.sharedInstance().untag("mech"); 
       Pushbots.sharedInstance().untag("ece"); 
       Pushbots.sharedInstance().untag("eee"); 
       editor.putString("session", "cse").commit(); 
       editor.commit(); 
       startActivity(show); 


      } 
     }); 
     clickButton1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       Intent show = new Intent(getApplicationContext(), noti_mech.class); 

       Pushbots.sharedInstance().tag("mech"); 
       Pushbots.sharedInstance().untag("ece"); 
       Pushbots.sharedInstance().untag("eee"); 
       Pushbots.sharedInstance().untag("cse"); 
       editor.putString("session", "mech").commit(); 
       editor.commit(); 

       startActivity(show); 


      } 
     }); 
     clickButton2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       Intent show = new Intent(getApplicationContext(), noti_ece.class); 

       Pushbots.sharedInstance().tag("ece"); 
       Pushbots.sharedInstance().untag("mech"); 
       Pushbots.sharedInstance().untag("eee"); 
       Pushbots.sharedInstance().untag("cse"); 
       editor.putString("session", "ec").commit(); 
       editor.commit(); 
       startActivity(show); 


      } 
     }); 
     clickButton3.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       Intent show = new Intent(getApplicationContext(), noti_eee.class); 

       Pushbots.sharedInstance().tag("eee"); 
       Pushbots.sharedInstance().untag("ece"); 
       Pushbots.sharedInstance().untag("mech"); 
       Pushbots.sharedInstance().untag("cse"); 
       editor.putString("session", "eee").commit(); 
       editor.commit(); 

       startActivity(show); 


      } 
     }); 
     onStartUp(); 



    } 
    private void onStartUp() 
    { 
     SharedPreferences sharedPreferences = getSharedPreferences("com.mateoj.multiactivitydrawer", Context.MODE_PRIVATE); 
     String str = sharedPreferences.getString("session", ""); 
     if (str.equals("cs")) { 
      clickButton.setClickable(true); 
      clickButton1.setClickable(false); 
      clickButton2.setClickable(false); 
      clickButton3.setClickable(false); 

     } else if (str.equals("mech")) { 
      clickButton.setClickable(false); 
      clickButton1.setClickable(true); 
      clickButton2.setClickable(false); 
      clickButton3.setClickable(false); 
     } else if (str.equals("ec")) { 
      clickButton.setClickable(false); 
      clickButton1.setClickable(false); 
      clickButton2.setClickable(true); 
      clickButton3.setClickable(false); 
     } else if (str.equals("eee")) { 
      clickButton.setClickable(false); 
      clickButton1.setClickable(false); 
      clickButton2.setClickable(false); 
      clickButton3.setClickable(true); 
     } 

    } 














    /* private class MyWebViewClient extends WebViewClient { 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      view.loadUrl(url); 
      return true; 
     } 

    } */ 



    @Override 
    protected boolean useDrawerToggle() { 
     return false; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (item.getItemId() == R.id.action_credits) 
      return true; 

     if (item.getItemId() == android.R.id.home) 
      onBackPressed(); 

     return super.onOptionsItemSelected(item); 
    } 


} 

那麼應該怎麼改變了這裏

+0

http://stackoverflow.com/questions/23720313/android-save-user-session只要按照這個問題 –

+0

@MaheshGiri請檢查上面的代碼 – sunny

回答

0

可以使用SharedPreferences類來保存用戶selection.This視頻可以幫助你開始 - https://www.youtube.com/watch?v=riyMQiHY3V4。只需將用戶選擇的按鈕保存在帶有sharedpreferences類的鍵值對中。在活動重新啓動或重新啓動時,只需檢查共享首選項實例中的值,並相應地設置按鈕setClickable(true)或setClickable(false)。

編輯:

clickButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
       //same code as above without the setClickable() on buttons 
      //remove the button.setClickable(false); for rest of the buttons 
      editor.putString("session", "akash").commit(); 
      editor.commit(); 
      startActivity(show); //Start the activity here 
     } 
}): 

重複此爲所有的點擊監聽器和改變「阿卡什」,以不同的東西(根據需要)爲每個按鈕單擊listener.Then使用方法

private void onStartUp(){ 
    SharedPreferences sharedPreferences = getSharedPreferences("com.mateoj.multiactivitydrawer", Context.MODE_PRIVATE); 
    String str=sharedPreferences .getString("session","akash"); 
    if(str.equals("akash")){ 
    //set buttons setClickable(true) or setClickable(false) 
    } else if(str.equals("..."))  //replace "..." with other strings which can replace "akash" 
    //set buttons setClickable(true) or setClickable(false) 
} //continue the else if logic till all conditions are met 
} 

call onStartUp();在onCreate();在爲按鈕設置點擊監聽器之後。

+0

如何做到這一點,你可以進行更改ñ告訴我 – sunny

+0

@sunny查看編輯的答案。 –

+0

在哪裏寫onstartup代碼 – sunny

0

有一兩件事你可以做的是保存Button的狀態(每次你/在Button用戶點擊保存truefalseSharedPreferences),每次啓動ActivitysetClickable()值把你SharedPrefernce結果。

這是一個選項,但有很多方法可以做到,那就是其中之一。

+0

可以ü請做出更改ñ顯示,以便我可以看到是否工作 – sunny

+0

我已經對代碼仍然不工作的更改請參閱 – sunny

0

我不知道你正在使用的這個框架(Pushbots),但我認爲你在forCreate()方法中試圖檢查在Pushbots中保存哪個標籤,並根據它啓用或禁用按鈕。您將標籤保存在Pushbots按鈕上,從不檢查保存在哪個標籤上以啓用或禁用該按鈕。

我建議你直接使用SharedPreferences而不是使用這個框架,即使我不知道它。 SharedPreferences使用起來非常簡單,我沒有看到任何使用框架的理由。

+0

請進行更改ñ顯示如何 – sunny