抱歉,我是新来的,这是我的第一篇帖子,很抱歉。
我希望通过点击按钮从radiobutton
获取价值,但仍有错误。为什么呢?
package com.example.jacobyogo.converter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
public class currency_conv extends AppCompatActivity {
EditText editDollar;
RadioButton radioRupiah;
RadioButton radioSg;
TextView txtResult;
Button btnConvert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_currency_conv);
editDollar = (EditText)findViewById(R.id.editDollar);
radioRupiah = (RadioButton)findViewById(R.id.radioRupiah);
radioSg = (RadioButton)findViewById(R.id.radioSg);
txtResult = (TextView)findViewById(R.id.txtHasil);
btnConvert = (Button)findViewById(R.id.btnConvert);
btnConvert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.radioRupiah:
if (checked)
converttorupiah();
break;
case R.id.radioSg:
if (checked)
converttosg();
break;
}
}
});
};
public void converttorupiah() {
double val = Double.parseDouble(editDollar.getText().toString());
txtResult.setText(Double.toString(val*13000));
}
public void converttosg() {
double val = Double.parseDouble(editDollar.getText().toString());
txtResult.setText(Double.toString(val*9000));
}}
答案 0 :(得分:2)
RadioButton
视图是您的按钮,将其转换为radioRupiah.isChecked()
毫无意义。您应该在需要的地方使用radioSg
和if (radioRupiah.isChecked())
converttorupiah();
else
converttosg();
。也
我认为最好这样做而不是切换案例:
{{1}}