基本上,我有一个由片段组成的导航栏活动,我正在制作一个单位转换器。我有一个如何清除的概念,只有一个简单的问题。由于我使用的是radiobuttons,每次我为同一单位转换更改要转换的值时,我需要点击不同的单选按钮,然后单击同一个单选按钮
final RadioGroup radioGroup = (RadioGroup) myView.findViewById(R.id.radio);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, final int checkedId) {
// checkedId is the RadioButton selected
switch (checkedId) {
case R.id.radioGram:
final TextView textView = (TextView) myView.findViewById(R.id.EditTextFrom);
textView.setText("Gram");
final RadioGroup radioGroup = (RadioGroup) myView.findViewById(R.id.radio2);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId2) {
switch (checkedId2) {
case R.id.radioGram2:
TextView textView = (TextView) myView.findViewById(R.id.EditTextTo);
textView.setText("Gram");
EditText calc = (EditText) myView.findViewById(R.id.From);
String gramholder = calc.getText().toString();
gram = Integer.parseInt(gramholder) * 1;
EditText editText = (EditText) myView.findViewById(R.id.To);
editText.setText(String.valueOf(gram));
break;
case R.id.radioKilogram2:
TextView textView2 = (TextView) myView.findViewById(R.id.EditTextTo);
textView2.setText("Kilogram");
EditText calc2 = (EditText) myView.findViewById(R.id.From);
String gramholder2 = calc2.getText().toString();
gram2 = Integer.parseInt(gramholder2) * 0.001;
EditText editText2 = (EditText) myView.findViewById(R.id.To);
editText2.setText(String.valueOf(gram2));
break;
}
}
});
因此,例如,如果我想将500克转换成千克,我只需点击radiobuttons克(在第一个放射组中)和千克(在第二个放射组中),但是如果我想要转换520克到公斤,我需要点击克然后再回到第二个无线电组的公斤
答案 0 :(得分:0)
这种情况正在发生,因为您只是通过仅收听CheckedChangeListener的Radio Buttons来更改editText的值。
您只需在editText中添加TextChangeListener并使用TextWatcher来监听更改。
然后,您不需要再次单击单选按钮。
编辑:写了这段代码。没有测试它,但可能足以理解解决方案。
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
/**
* Created by Kartik Sharma on 01/01/17.
*/
public class Stack extends Activity implements RadioGroup.OnCheckedChangeListener{
EditText editText_From, editText_To;
RadioGroup radioGroup_From, radioGroup_To;
View myView;
private final String GRAM = "GRAM", KILOGRAM = "KILOGRAM";
String currentSelectedFrom = GRAM, currentSelectedTo = GRAM;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myView = findViewById(R.id.fragment);
editText_From = (EditText) myView.findViewById(R.id.From);
editText_To = (EditText) myView.findViewById(R.id.To);
radioGroup_From = (RadioGroup) findViewById(R.id.radio);
radioGroup_To = (RadioGroup) myView.findViewById(R.id.radio2);
editText_From.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(currentSelectedTo == KILOGRAM){
double gram2 = Integer.parseInt(s.toString()) * 0.001;
editText_To.setText(String.valueOf(gram2));
}else if(currentSelectedTo == GRAM){
double gram = Integer.parseInt(s.toString()) * 1;
editText_To.setText(String.valueOf(gram));
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
//SIMILARLY FOR EDITTEXT_TO IF REVERSE CALCULATION IS NEEDED
radioGroup_From.setOnCheckedChangeListener(this);
radioGroup_To.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(group == radioGroup_From){
switch(checkedId){
case R.id.radioGram :
//PERSON SELECTED GRAM IN FROM SECTION
final TextView textView = (TextView) myView.findViewById(R.id.EditTextFrom);
textView.setText("Gram");
currentSelectedFrom = GRAM;
break;
}
}else if(group == radioGroup_To){
switch (checkedId) {
case R.id.radioGram2:
//PERSON SELECTED GRAM IN TO SECTION
TextView textView = (TextView) myView.findViewById(R.id.EditTextTo);
textView.setText("Gram");
String gramholder = editText_From.getText().toString();
double gram = Integer.parseInt(gramholder) * 1;
editText_To.setText(String.valueOf(gram));
currentSelectedTo = GRAM;
break;
case R.id.radioKilogram2:
//PERSON SELECTED KILOGRAM IN TO SECTION
TextView textView2 = (TextView) myView.findViewById(R.id.EditTextTo);
textView2.setText("Kilogram");
String gramholder2 = editText_From.getText().toString();
double gram2 = Integer.parseInt(gramholder2) * 0.001;
editText_To.setText(String.valueOf(gram2));
currentSelectedTo = KILOGRAM;
break;
}
}
}
}