2016-08-16 5 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 (фа ЛСЭ) в 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); ; 
     } 
Смежные вопросы