2014-12-15 4 views
2

버튼을 클릭하면 5 개의 라디오 버튼이있는 팝업 대화 상자가 표시됩니다. 지금 요구 사항은이전에 선택한 라디오 버튼 상태를 저장하는 방법과 응용 프로그램이 종료되고 시작될 때 항상 처음 선택된 상태로 저장하는 방법은 무엇입니까?

  1. 입니다. 버튼을 클릭하면 처음에는 선택되지 않은 상태의 체크 표시가있는 라디오 버튼과 확인되지 않은 상태의 첫 라디오 버튼이 표시됩니다.
  2. 다음 라디오 단추를 선택하여 사용자가 변경되면 대화 상자를 닫고 사용자가 팝업을 열면 다시 선택됩니다. 이전에 선택 상태 여야합니다.

어떻게 진행하나요?

public class PopUpDialogRadioButton extends Activity { 
    private Button popUpClick; 
    private Dialog dialog; 
    private RadioGroup radioGroup; 
    private RadioButton radioButton1, radioButton2, radioButton3, radioButton4, radioButton5; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.sortfilterclick); 
    popUpClick = (Button) findViewById(R.id.popupButton); // on click of button, opens a pop up dialog 
    popUpClick.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      dialog = new Dialog(PopUpDialogRadioButton.this); 
      dialog.setContentView(R.layout.sortfilterrow); 
      radioGroup = (RadioGroup) dialog.findViewById(R.id.radioGroup1); 
      radioButton1 = (RadioButton) dialog.findViewById(R.id.radio1); 
      radioButton2 = (RadioButton) dialog.findViewById(R.id.radio2); 
      radioButton3 = (RadioButton) dialog.findViewById(R.id.radio3); 
      radioButton4 = (RadioButton) dialog.findViewById(R.id.radio4); 
      radioButton5 = (RadioButton) dialog.findViewById(R.id.radio5); 
      radioButton1.setOnClickListener(radioButtonOnClickListener); 
      radioButton2.setOnClickListener(radioButtonOnClickListener); 
      radioButton3.setOnClickListener(radioButtonOnClickListener); 
      radioButton4.setOnClickListener(radioButtonOnClickListener); 
      radioButton5.setOnClickListener(radioButtonOnClickListener); 
      radioGroup.clearCheck();  
      radioButton1.setChecked(true); 
      int selectedId = radioGroup.getCheckedRadioButtonId(); 
      RadioButton osButton = (RadioButton) dialog 
        .findViewById(selectedId); 
      StringBuffer responseText = new StringBuffer(); 
      responseText.append(osButton.getText()); 
      Toast.makeText(getApplicationContext(), responseText, 
        Toast.LENGTH_SHORT).show(); 
      // radioGroup.setOnCheckedChangeListener(radioGroupOnCheckedChangeListener); 

      dialog.setCancelable(true); 
      dialog.setTitle("Sort By"); 
      dialog.show(); 
     } 

     private final OnClickListener radioButtonOnClickListener = new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       /* 
       * RadioButton rb = (RadioButton) v; 
       * Toast.makeText(SortFilterPopupActivity.this, 
       * rb.getText(), Toast.LENGTH_SHORT).show(); 
       */ 
       switch (v.getId()) { 
       case R.id.radio1: 
        Toast.makeText(PopUpDialogRadioButton.this, "first", 
          Toast.LENGTH_SHORT).show(); 
              dialog.dismiss(); 
        break; 
       case R.id.radio2: 
        Toast.makeText(PopUpDialogRadioButton.this, "two", 
          Toast.LENGTH_SHORT).show(); 
              dialog.dismiss(); 
        break; 
       case R.id.radio3: 
        Toast.makeText(PopUpDialogRadioButton.this, "three", 
          Toast.LENGTH_SHORT).show(); 
              dialog.dismiss(); 
        break; 
       case R.id.radio4: 
        Toast.makeText(PopUpDialogRadioButton.this, "four", 
          Toast.LENGTH_SHORT).show(); 
             dialog.dismiss(); 
        break; 
       case R.id.radio5: 
        Toast.makeText(PopUpDialogRadioButton.this, "five", 
          Toast.LENGTH_SHORT).show(); 
        dialog.dismiss(); 
        break; 
       } 
      } 
     }; 
    }); 
} 
+0

.. –

답변

4

당신은이에 대한 SharedPreferences를 사용해야합니다 다음과 같이

코드입니다.

5 개의 라디오 버튼이 있으므로 이전에 대화 상자를 사용하지 않은 경우 SharedPreferences을 기본값 (예 : 0)으로 사용할 수 있습니다.

int radio_selected = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE).getInt("my_selected_radio",0); 

그런 다음 OnClickListener을 사용하여 선택한 값을 확인하고 저장할 수 있습니다.

는 희망이 도움이 원하는 행동을 달성하기에 충분해야

int radio_selected = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE).edit().putInt("my_selected_radio",selectedValue).commit(); 

!

0

SharedPreferences에 선택한 항목을 저장할 수 있습니다. 좋은 개발자 사이트를 확인하려면 introduction to SharedPreferences.

샘플 사용 : 환경 설정에서 선택과 때마다 체크 박스 다음에 팝업을 표시하는 환경 설정에서 선택한 체크 박스를 다시 저장

// Loading preferences. 
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
    String selectedItem = settings.getString("chkboxSelection"); 
    if (selectedItem != null) { 
     // Compare value and mark checkbox accordingly. 
    } 

    // Saving preferences. 
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
    SharedPreferences.Editor editor = settings.edit(); 
    editor.putString("chkboxSelection","checkboxIdentifier"); 
관련 문제