我有一个edittext框和2radio按钮,一个是daysradiobutton,另一个是monthsradiobutton。 在edittextbox中输入数字,然后单击输入数字的一个radiobutton月份转换为天数并显示在同一个edittext框中。请帮助我
答案 0 :(得分:0)
xml layout:
<?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));
}
}
});
}
答案 1 :(得分:0)
在单选按钮上设置on checked changed listener:
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
}
}
};