2016-08-16 74 views
-1

在我的應用程序中,我有2個按鈕。
開始遊戲中的第一個按鈕和繼續遊戲中的第二個按鈕。
我想在應用程序運行時第一次禁用第二個按鈕。
這意味着第一次我的第二個按鈕被禁用到onClick。如何處理這個?
我怎樣才能讓它明白這是第一次?應用程序第一次運行時如何禁用單擊按鈕

public class Menu extends Activity implements View.OnClickListener { 

SharedPreferences prefs; 
Editor edit; 
TextView best; 
private boolean flag; 
public static ImageView btn1, conti, but3, but4; 
static Noti_Queue noti_queue; 
static Splash splash; 
public static AppList applist; 
public static int userId; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.home_activity); 

    flag = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("flag", false); 


    if (applist == null) { 
     applist = new AppList(this); 
     applist.set_identity("1"); 
    } 

    if (splash == null) { 
     splash = new Splash(this); 
     splash.set_identity("1"); 
    } 

    if (noti_queue == null) { 
     noti_queue = new Noti_Queue(this); 
     noti_queue.set_identity("1"); 
    } 


    btn1 = (ImageView) findViewById(R.id.button1); 
    conti = (ImageView) findViewById(R.id.btn2); 
    but3 = (ImageView) findViewById(R.id.button3); 
    but4 = (ImageView) findViewById(R.id.button4); 

    btn1.setOnClickListener(this); 
    conti.setOnClickListener(this); 
    conti.setBackgroundResource(R.drawable.cont); 
    if (!flag) { 
     flag = true; 
     conti.setBackgroundResource(R.drawable.cont_press);} 
    conti.setEnabled(flag); 
    but3.setOnClickListener(this); 
    but4.setOnClickListener(this); 


    // giving question id 
    final int que_id = getIntent().getIntExtra("integer", 0); 
    Log.e("mhs", que_id + ""); 
    //now lets save the que_id(now it is save to SharedPreferences 
    SharedPreferences myPrefs = getSharedPreferences("myPrefs", MODE_WORLD_READABLE); 
    userId = myPrefs.getInt("userId", 0); 
    //let get it to show 

    Log.e("saved", que_id + ""); 

    setsize(); 
    best = (TextView) findViewById(R.id.textView1); 
    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf"); 
    best.setTypeface(tf, Typeface.BOLD); 
    prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
    edit = prefs.edit(); 
    if (prefs.getInt("Best", 0) <= 0) { 
     edit.putInt("Best", 0); 
     edit.commit(); 
    } 
    best.setText("بیشترین امتیاز : " + prefs.getInt("Best", 0)); 
} 


@Override 
public void onBackPressed() { 
    splash.Display(); 
    splash = null; 
    super.onBackPressed(); 
} 


@Override 
protected void onResume() { 
    best.setText("بیشترین امتیاز : " + prefs.getInt("Best", 0)); 
    super.onResume(); 
} 

private void setsize() { 


    DisplayMetrics dm = new DisplayMetrics(); 

    getWindowManager().getDefaultDisplay().getMetrics(dm); 

    int h = dm.heightPixels; 
    int w = dm.widthPixels; 

    h = h/6; 
    w = w - ((w * 30)/100); 


    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(w, h); 

    but4.setLayoutParams(params); 
    but3.setLayoutParams(params); 
    btn1.setLayoutParams(params); 
    conti.setLayoutParams(params); 

} 


@Override 
public void onClick(View v) { 

    switch (v.getId()) { 


     case R.id.button1: 
      if (!flag) { 
       flag = true; 
       PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("flag", flag).commit(); 
       conti.setEnabled(flag); 
       conti.setBackgroundResource(R.drawable.cont_press); 
       Intent intent = new Intent(this, MainActivity.class); 
       startActivity(intent); 
       //finishing this activity is important to exit from app 
       Menu.this.finish(); 
      } 
      break; 

     case R.id.btn2: 
      Toast.makeText(getApplicationContext(), userId + "", Toast.LENGTH_LONG).show(); 
      Intent intent1 = new Intent(Menu.this, ContinueActivity.class); 
      intent1.putExtra("integer2", userId); 
      startActivity(intent1); 
      Menu.this.finish(); 
      break; 
     case R.id.button3: 
      Toast.makeText(getApplicationContext(), "help", Toast.LENGTH_LONG).show(); 
      break; 
     case R.id.button4: 
      applist.Display(); 
      break; 
    } 
} 
+1

使用button.setEnabled(假)在onCreateActivity – Manish

+0

@ManishYadav我想你不明白我的問題,我只是想只是第一次 – nsr

+0

使用button.setEnabled(FA LSE)在onCreateActivity然後的setEnabled(真)當開始按鈕點擊..... – Manish

回答

0

STEP1: -聲明Boolean全局變量和SharePrefernce ....

private boolean flag=PreferenceManager.getDefaultSharedPreferences(this).getBoolean("flag", false); 

STEP2獲得的價值: -稱這種現象onCreate()方法...

conti.setEnabled(flag); 

STEP3: -坐落在的onClick()方法....

case: R.id.button1: 
if(!flag){ 
    flag=true; 
    conti.setEnabled(flag); 
    PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("flag", flag).commit(); 
    } 
break; 

STEP4: -最後使用sharePrefernce存儲該boolean變量.....

+0

我不想讓它隱形 – nsr

+0

只是使'btn2.setEnabled(fa lse)' – sushildlh

+0

沒有任何變化,在那裏我第二次啓用我的按鈕 – nsr

0

的幫助下sharedprefrence可以實現這個例子。

SharedPreferences sharedPreferences = getSharedPreferences("myPref", MODE_PRIVATE); 
     SharedPreferences.Editor editor = sharedPreferences.edit(); 
     editor.putBoolean("enableButton", true); 
     editor.commit(); 

把這個代碼放在啓用按鈕。

SharedPreferences sharedPreferences = getSharedPreferences("myPref", MODE_PRIVATE); 
     flag = sharedPreferences.getBoolean("enableButton", false); 
     if (flag) { 
      conti.setEnabled(true); ; 
     } 
相關問題