Android studio 在android studio中填充float类型编辑文本的语音输入
我有一门课来计算体重指数。我想允许用户通过语音输入他/她的体重和身高。但是对于activityResult函数,我很困惑该如何做Android studio 在android studio中填充float类型编辑文本的语音输入,android-studio,input,android-edittext,voice-recognition,user-interaction,Android Studio,Input,Android Edittext,Voice Recognition,User Interaction,我有一门课来计算体重指数。我想允许用户通过语音输入他/她的体重和身高。但是对于activityResult函数,我很困惑该如何做 public class BMIActivity extends AppCompatActivity { //speech field private static final int REQUEST_CODE_SPEECH_INPUT=1000; //speech TextView editText1; TextView editText2; //views
public class BMIActivity extends AppCompatActivity {
//speech field
private static final int REQUEST_CODE_SPEECH_INPUT=1000;
//speech
TextView editText1;
TextView editText2;
//views for speech
FloatingActionButton mVoiceBtn;
TextView mVoiceInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b_m_i);
EditText weight,height;
TextView txtRes,txtEnter;
Button btnRes,btnReset;
weight=findViewById(R.id.weight);
height=findViewById(R.id.height);
txtEnter= findViewById(R.id.txtEnter);
txtRes= findViewById(R.id.txtRes);
btnRes=findViewById(R.id.btnRes);
btnReset=findViewById(R.id.btnReset);
btnRes.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String strweg=weight.getText().toString();
String strhei=height.getText().toString();
//hooks for shake detector
editText1=(TextView) findViewById(R.id.weight);
editText2=(TextView) findViewById(R.id.height);
set_btn=findViewById(R.id.setBtn);
//speech hooks
mVoiceBtn = findViewById(R.id.voiceBtn);
mVoiceInput = findViewById(R.id.voiceInput);
if(strweg.equals("")){
weight.setError("Please Enter Your Weight");
weight.requestFocus();
return;
}
if(strhei.equals("")){
height.setError("Please Enter Your Height");
height.requestFocus();
return;
}
float wgt = Float.parseFloat(strweg);
float hgt = Float.parseFloat(strhei)/100;
float bmiValue = BMICalculate(wgt,hgt);
txtEnter.setText(interpreteBMI(bmiValue));
txtRes.setText("BMI = "+bmiValue);
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
height.setText("");
weight.setText("");
txtEnter.setText("");
txtRes.setText("");
}
});
}
//在这里,我遇到了一个问题,通过单击麦克风按钮,允许用户首先通过语音输入重量,然后通过语音本身输入高度
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
//get text array for voice intent
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
mVoiceInput.setText(result.get(0));
editText1.setText(result.get(0));
}
break;
}
}
}
public float BMICalculate(float weight, float height){
return weight / (height*height);
}
public String interpreteBMI(float bmiValue){
if(bmiValue<16){
return "Severely underweight";
}
else if(bmiValue<18.5){
return "Underweight";
}
else if(bmiValue<25){
return "Normal";
}
else if(bmiValue<30){
return "Overweight";
}
else{
return "Obese";
}
}
@覆盖
受保护的void onActivityResult(int-requestCode、int-resultCode、@Nullable-Intent-data){
super.onActivityResult(请求代码、结果代码、数据);
开关(请求代码){
案例请求\代码\语音\输入:{
if(resultCode==RESULT\u OK&&null!=数据){
//获取语音意图的文本数组
ArrayList结果=data.getStringArrayListExtra(RecognizerIntent.EXTRA_结果);
mVoiceInput.setText(result.get(0));
editText1.setText(result.get(0));
}
打破
}
}
}
公共浮子BMI计算(浮子重量、浮子高度){
返回重量/(高度*高度);
}
公共字符串解释器BMI(浮点BMI值){
if(bmi)值