在编辑文本框中输入数字,并在此基础上选中其中一个单选按钮



我有一个编辑文本框和2个单选按钮,一个是daysradiobutton,另一个是monthsradiobutton。在编辑文本框中输入数字,然后单击其中一个单选按钮月份,输入的数字将转换为天并显示在同一编辑文本框内。请帮助我

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));
                }
            }
        });
    }

在单选按钮上设置已检查的已更改侦听器:

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
        }
    }
};

相关内容

最新更新