1
我有一个edittext框和2radio按钮之一是daysradiobutton,另一个是monthsradiobutton。在edittextbox 输入号码,然后点击的那输入的号码转换为天,并显示在同一个EditText上box.Plese帮我在编辑文本框中输入数字,并检查其中一个单选按钮
我有一个edittext框和2radio按钮之一是daysradiobutton,另一个是monthsradiobutton。在edittextbox 输入号码,然后点击的那输入的号码转换为天,并显示在同一个EditText上box.Plese帮我在编辑文本框中输入数字,并检查其中一个单选按钮
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
}
}
};
听起来作业问题。 – Lucifer 2012-02-11 12:20:24