我有这个表格供用户填写,其中一个编辑文本是BMI,我想在用户输入高度和重量而不点击任何按钮后是否可以自动填充BMI编辑文本。我用Google搜索并发现了一个名为Text Watcher的东西。我尝试在我的代码中实现它,但是当我运行应用程序时,BMI的编辑文本仍然是空的,数据库也是如此。
package mdad.project;
import com.example.manandhowproject.R;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Userreg extends Activity implements TextWatcher {
SQLiteDatabase db;
Button btnNext;
EditText etName, etAge, etHeight, etWeight, etBMI;
double result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userreg);
etName = (EditText) findViewById(R.id.etName);
etAge = (EditText) findViewById(R.id.etAge);
etHeight = (EditText) findViewById(R.id.etHeight);
etWeight = (EditText) findViewById(R.id.etWeight);
etBMI = (EditText) findViewById(R.id.etBMI);
Button button = (Button) findViewById(R.id.btnSignOut);
button.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
Toast.makeText(getApplicationContext(), "Log Out Success ! ", Toast.LENGTH_LONG).show();
Intent msg2 = new Intent(Userreg.this, Login.class);
startActivity(msg2);
}
});
Button btnNext = (Button) findViewById(R.id.btnNext);
btnNext.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
String name = etName.getText().toString();
String age = etAge.getText().toString();
int nAge = new Integer(age).intValue();
String height = etHeight.getText().toString();
int nHeight = new Integer(height).intValue();
String weight = etWeight.getText().toString();
int nWeight = new Integer(weight).intValue();
String bmi = etBMI.getText().toString();
String sql = "insert into UserInfo (Name,Age,Height,Weight,BMI) values( '" + name + "','" + nAge + "','" + nHeight + "', '" + nWeight + "', '" + bmi + "')";
String result = updateTable(sql);
Intent msg3 = new Intent(Userreg.this, Regsuccess.class);
startActivity(msg3);
}
});
String sql = "create table if not exists UserInfo (recld integer PRIMARY KEY autoincrement, Name text, Age text, Height text, Weight text, BMI text )";
String result = createDatabase(sql, "UserInf.db");
}
private void calculate() {
String height = etHeight.getText().toString();
int nHeight = new Integer(height).intValue();
String weight = etWeight.getText().toString();
int nWeight = new Integer(weight).intValue();
result = nWeight / (nHeight * nHeight);
String textResult = "" + result;
etBMI.setText(textResult);
}
String createDatabase(String sql, String dbName) {
try {
System.out.println(sql);
db = SQLiteDatabase.openOrCreateDatabase("sdcard/" + dbName, null);
db.beginTransaction();
db.execSQL(sql);
db.setTransactionSuccessful();
db.endTransaction();
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
System.out.println(e.toString());
return ("error open DB");
}
return "";
}
String updateTable(String sql) {
try {
System.out.println(sql);
db.beginTransaction();
db.execSQL(sql);
db.setTransactionSuccessful();
db.endTransaction();
} catch (Exception e) {
System.out.println(e.toString());
return ("Error updating DB");
}
return ("DB updated");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
calculate();
}
}
答案 0 :(得分:0)
你可以用两种方式做到这一点,
第一次使用TextWatcher
,因为每个人都在提供解决方案。和,
SECOND 方式是,使用OnFocusChangeListener
进行EditText。以下是如何实现它的示例,
EditText one = findViewById(R.id.one);
EditText two = findViewById(R.id.two);
EditText three = findViewById(R.id.three);
three.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus){
Double ONE = Double.parseDouble(one.getText().toString().trim());
Double TWO = Double.parseDouble(two.getText().toString().trim());
Double THREE = ONE+TWO;
three.setText(String.valueOf(THREE));
Log.i("tag", "onFocusChange: "+THREE);
//or you can perform any other task
}
}
});
如上例所示,当用户将注意力集中在third
EditText时,它会从first
和second
EditText中获取值并显示结果。
答案 1 :(得分:-1)
你错过了
etHeight.addTextChangedListener(this);
etWeight.addTextChangedListener(this);
onCreate()
中的。
另外,更新您的calculate()
方法,因为它包含有关计算BMI的错误:
private void calculate() {
try {
String height = etHeight.getText().toString();
int nHeight = Integer.parseInt(height);
String weight = etWeight.getText().toString();
int nWeight = Integer.parseInt(weight);
result = nWeight / (nHeight * nHeight);
String textResult = "" + result;
etBMI.setText(textResult);
}
catch (NumberFormatException e) {
etBMI.setText("");
}
}