2012-05-16 55 views
0

我是Android開發的新手。 讓我解釋我的問題。 我在主要活動中有一個複選框和一個按鈕。當我檢查(即啓用)我的複選框並點擊下面的按鈕時,我將移動到另一個活動,並且當我在第二個活動中單擊一個按鈕時,我會回到我的主活動。我的問題是,當我回來時,複選框保持未檢查或禁用。即使從第二次活動回來後,我怎樣才能保持其狀態?請幫忙。即使導航到android中的不同活動後,如何維護CheckBox的狀態?

XML佈局對應的複選框的部分是:

<CheckBox 
    android:id="@+id/ckBxAll" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="ON" /> 

爲主要業務骨架代碼:

Public class TestActivity extends Activity { 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
LayoutInflater mInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); 
View mMainView = mInflater.inflate(R.layout.main, null);   
setContentView(mMainView); 
Enable_chkbox = (CheckBox)mMainView.findViewById(R.id.ckBxAll); 
if(b_onResume==true) 
    { 
     Enable_chkbox.setChecked(b_onResume); 
    } 
Enable_chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       // TODO Auto-generated method stub 




//do something 



     });//Enable_chkbox 
Compose_btn=(Button)findViewById(R.id.btnCompose); 
     Compose_btn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent gotoComposeMessage=new Intent(TestActivity.this,ComposeMessage.class); 
       startActivity(gotoComposeMessage); 
      } 
     }); 
} 
@Override 
    protected void onResume() { 
     super.onResume(); 
    b_onResume= Enable_chkbox.isChecked(); 

    } 
    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     unregisterReceiver(myreceiver); 
     this.finish(); 
     } 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_BACK) { 
      moveTaskToBack(true); 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 
    @Override 
    public void onSaveInstanceState(Bundle savedInstanceState) { 
     super.onSaveInstanceState(savedInstanceState); 
     savedInstanceState.putBoolean("Enable_chkbox",true);  

    } 
    @Override 
    protected void onRestoreInstanceState(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onRestoreInstanceState(savedInstanceState); 
     boolean myBoolean = savedInstanceState.getBoolean("Enable_chkbox"); 
    } 

} 

我的第二活動:

public class ComposeMessage extends Activity{ 
    Button SaveSMS_btn; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.composemessage); 
     SaveSMS_btn=(Button)findViewById(R.id.btnSave); 
     SaveSMS_btn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent gotoHome=new Intent(getApplicationContext(),BlockTestActivity.class); 
       Bundle BundleToCarryMessage = new Bundle(); 
       EditText EnterSMS_edt=(EditText)findViewById(R.id.edtEnterSMS); 
       BundleToCarryMessage.putString("Message", EnterSMS_edt.getText().toString()); 
       gotoHome.putExtras(BundleToCarryMessage); 
       startActivity(gotoHome); 
      } 
     }); 

    } 

} 

在第二使用的XML文件活動:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <EditText 
     android:id="@+id/edtEnterSMS" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textMultiLine" > 

     <requestFocus /> 
    </EditText> 

    <Button 
     android:id="@+id/btnSave" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Save" /> 

</LinearLayout> 
+0

顯示我的代碼如何在活動之間進行導航.. –

+0

hi RajaReddy,我只使用intents導航.. – Deepthi

回答

5

還有另一種方法。 你需要重寫的onSaveInstanceState(捆綁savedInstanceState),並寫上你想改變這樣的捆綁參數的應用程序的狀態值:

下面的代碼是用於保存複選框的狀態,移動到第二活動之前:

@Override 
public void onSaveInstanceState(Bundle savedInstanceState) { 

    savedInstanceState.putBoolean("MyCheckBox", enable); 

    super.onSaveInstanceState(savedInstanceState); 
} 

這是當你恢復複選框的狀態:

@Override 
public void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 

    boolean myBoolean = savedInstanceState.getBoolean("MyCheckBox"); 

} 

編輯:

public class TestActivity extends Activity implements OnCheckedChangeListener, OnClickListener { 

     private CheckBox Enable_chkbox; 
     private View mMainView; 
     private Button Compose_btn; 
     private boolean myBoolean = false;; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      LayoutInflater mInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); 
      mMainView = mInflater.inflate(R.layout.main, null);   
      setContentView(mMainView); 
      initView(); 
     } 

     private void initView(){ 

      Enable_chkbox = (CheckBox)mMainView.findViewById(R.id.ckBxAll); 
      Enable_chkbox.setOnCheckedChangeListener(this); 
      Compose_btn=(Button)mMainView.findViewById(R.id.btnCompose); 
      Compose_btn.setOnClickListener(this); 
     } 

     @Override 
     protected void onResume() { 
      super.onResume(); 
      Enable_chkbox.setChecked(myBoolean); 

     } 

     @Override 
     public void onSaveInstanceState(Bundle savedInstanceState) { 
      super.onSaveInstanceState(savedInstanceState); 
      savedInstanceState.putBoolean("Enable_chkbox", Enable_chkbox.isChecked());  
     } 

     @Override 
     protected void onRestoreInstanceState(Bundle savedInstanceState) { 
      super.onRestoreInstanceState(savedInstanceState); 
      myBoolean = savedInstanceState.getBoolean("Enable_chkbox"); 
     } 

     @Override 
     public void onClick(View arg0) { 
      //do something 
     } 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      //do something 
     } 
} 

'

希望它能幫助。

0

使用startActivityforResult切換到第二個活動。當你使用它切換時,你不完成當前活動,所以它存儲了活動的狀態。

2

對於所有一般用途,您必須在離開活動時保存複選框和單選按鈕的狀態,並在您回到活動時恢復它們。
爲此,我們通常使用共享首選項。我們保存onPause()方法中按鈕的狀態,並在onResume()方法中恢復狀態。
使用共享的喜好,你可以這樣做:

SharedPreferences prefs = this.getSharedPreferences(
     "com.example.app", Context.MODE_PRIVATE); 

閱讀喜好:

String dateTimeKey = "com.example.app.datetime"; 

// use a default value using new Date() 
long l = prefs.getLong(dateTimeKey, new Date().getTime()); 

要在this blog編輯和保存喜好

Date dt = getSomeDate(); 
prefs.edit().putLong(dateTimeKey, dt.getTime()).commit(); 

查看更多的例子。
通過將上述代碼置於正確的方法(onPause和onResume)中,您可以恢復按鈕的狀態。

+0

非常感謝您的幫助。我會試試這個。 – Deepthi

相關問題