2012-02-11 55 views

回答

0

XML布局的单选按钮的一个月份:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <EditText 
     android:layout_width="match_parent" 
     android:id="@+id/editText1" 
     android:inputType="number" 
     android:layout_height="wrap_content"></EditText> 
    <RadioGroup 
     android:id="@+id/radioGroup1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
     <RadioButton 
      android:layout_height="wrap_content" 
      android:id="@+id/radio0" 
      android:text="Day" 
      android:layout_width="wrap_content" 
      android:checked="true"></RadioButton> 
     <RadioButton 
      android:layout_height="wrap_content" 
      android:id="@+id/radio1" 
      android:text="Month" 
      android:layout_width="wrap_content"></RadioButton> 
    </RadioGroup> 
</LinearLayout> 

java代码:

int yourComp=30; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     final EditText et=(EditText) findViewById(R.id.editText1); 
     RadioButton rd0=(RadioButton) findViewById(R.id.radio0); 
     RadioButton rd1=(RadioButton) findViewById(R.id.radio1); 

     rd0.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if(isChecked) 
       { 
        int i=Integer.parseInt(et.getText().toString()); 
        //for example 
        et.setText(""+(yourComp+i)); 
       } 
      } 
     }); 

     rd1.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if(isChecked) 
       { 
        int i=Integer.parseInt(et.getText().toString()); 
        //example 
        et.setText(""+(yourComp-i)); 
       } 
      } 
     }); 
    } 
0

组检查改变的监听器上的单选按钮:

private OnCheckedChangeListener radio_listener = new OnCheckedChangeListener() { 
    onCheckedChanged(RadioGroup group, int checkedId) { 
     // Perform action on clicks 
     if(checkedId==rbDays.getId()) 
     { 
      String extracted=editText.getText().toString(); 
      int months= 0; 
      try 
      { 
       months=Integer.parseInt(extracted); 
      }catch(NumberFormatException ex) 
      { 
      } 
      //implement logic to convert days from months 
      int days=months*30; 
      editText.setText(""+days); 
     } 
     else if(checkedId==rbMonths.getId()) 
     { 
       //Implement logic as days 
     } 
    } 
}; 
相关问题