gpt4 book ai didi

java - 两个单选按钮均处于 Activity 状态

转载 作者:行者123 更新时间:2023-12-01 09:54:06 26 4
gpt4 key购买 nike

我对 Android 不太熟悉,正在尝试实现一个用于转换为摄氏度或华氏度的基本应用程序。

这是基本代码

public class MainActivity extends AppCompatActivity {

EditText temp ;
RadioButton celsius;
RadioButton fahrenheit;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

temp = (EditText) findViewById(R.id.question_text);
final RadioGroup options = (RadioGroup) findViewById(R.id.options);
celsius = (RadioButton) findViewById(R.id.celsius);
fahrenheit = (RadioButton) findViewById(R.id.fahrenheit);
button = (Button) findViewById(R.id.convert);

button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view){
int button1 = 1;
int button2 = 2;
celsius.setId(button1);
fahrenheit.setId(button2);
int number = Integer.parseInt(temp.getText().toString());
TextView text = (TextView) findViewById(R.id.answer_text);
double answer;
int id = options.getCheckedRadioButtonId();
if(id == -1){
text.setText("Please select an option");
}
else if(id == 2){
answer = number*1.8 + 32; //Convert to Fahrenheit
text.setText("" + answer);
}
else{
answer = (number-32)/1.8;
text.setText("" + answer); //Convert to Celsius
}
}
});
}

}

我实现了一个 RadioGroup,以便只能选择其中一个按钮

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android.temperatureconverter.MainActivity">

<EditText
android:id="@+id/question_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter the temperature."
android:inputType="number"
android:paddingLeft="18dp"
android:paddingTop="18dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="18dp"
android:paddingTop="18dp"
android:text="Convert to?"
android:textAllCaps="true"
android:textSize="24sp"
android:typeface="sans" />


<RadioGroup
android:id="@+id/options"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="18dp"
android:orientation="horizontal">

<RadioButton
android:id="@+id/celsius"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Celsius"
android:textSize="20sp" />

<RadioButton
android:id="@+id/fahrenheit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Fahrenheit"
android:textSize="20sp" />
</RadioGroup>

<Button
android:id="@+id/convert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CONVERT"
android:textSize="20sp"
android:typeface="monospace" />

<TextView
android:id="@+id/answer_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

该程序第一次运行正常。但如果我想再次转换它两个单选按钮都被选中,这是令人费解的。我对此真的很陌生,所以我想知道发生了什么事......

最佳答案

注释掉以下几行。

celsius.setId(button1);
fahrenheit.setId(button2);

这基本上就是问题所在。

此外,这将解决问题。

button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view){
int number = Integer.parseInt(temp.getText().toString());
TextView text = (TextView) findViewById(R.id.answer_text);
double answer;
int id=-1;
if(fahrenheit.isChecked())
{
id=1;
}
else if(celsius.isChecked())
{
id=2;
}
System.out.println("Selected option is "+id);
if(id == 1){
answer = number*1.8 + 32; //Convert to Fahrenheit
text.setText("" + answer);
}
else if (id ==2){
answer = (number-32)/1.8;
text.setText("" + answer); //Convert to Celsius
}
if(id == -1){
text.setText("Please select an option");
}

}
});

关于java - 两个单选按钮均处于 Activity 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37376077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com