2016-08-16 5 views
-1

私のアプリケーションには2つのボタンがあります。
開始ゲームの最初のボタンと継続ゲームの2番目のボタン。
私のアプリケーションが実行されたときに初めて2番目のボタンを無効にしたいとします。
初めて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

使用はonCreateActivity中(FA LSE)私はあなたが私の質問を理解していけないと思う – Manish

+0

@ManishYadavが、私はちょうどbutton.setEnabledちょうど初めて – nsr

+0

使用したいonCreateActivityに(偽)button.setEnabled、その後は、setEnabled(真) .....開始ボタンがクリックされたら..... – Manish

答えて

0

STEP1: - :...

onCreate()方法で呼び出しこの -はグローバル Boolean変数を宣言して SharePrefernce ....

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

STEP2から値を取得します

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: -が最後にこのboolean変数を格納するsharePrefernceを使用.....

+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); ; 
     } 
関連する問題