0
我對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>
該項目工程都在第一時間。但如果我想再次轉換它都單選按鈕正在被選中,這是令人費解的。我真的很新,所以我想知道發生了什麼...
這似乎是工作好!但我仍然不知道以前有什麼不對。另外,setSelected的目的到底是什麼? –
我只是複製/粘貼你的代碼。而在你的代碼中,'options.setSelected'沒有解決任何目的。對不起,但無法調試你的整個代碼,只是添加了一個我認爲可能有用的代碼片段。如果有幫助,請考慮接受答案。謝謝。 – driftking9987