[英]EditText - How to set default text in android edittext
I am working on an android application which people will enter their bills. 我正在开发一个Android应用程序,人们将输入帐单。 I have an EditText which people enter the amount. 我有一个EditText,人们可以输入金额。
What I want to do is that for example If a person enters "2" it should be converted automatically to "0.02". 我想做的是,例如,如果某人输入“ 2”,则应自动将其转换为“ 0.02”。 Then if he/she wants to enter 22$ he should push 2, 2, 0 and 0 buttons. 然后,如果他/她想输入22 $,则应按2、2、0和0按钮。 It will be like that "22.00". 就像“ 22.00”。 How can I manage to do that using EditText. 如何使用EditText做到这一点。 Can you give me some ideas guys? 你们能给我一些想法吗?
Here is the related code pieces : 这是相关的代码段:
<EditText
android:id="@+id/amount"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginTop="5dip"
android:inputType="number"
android:hint="@string/enter_amount"
android:singleLine="true" />
EditText payment = (EditText) findViewById(R.id.amount);
if (payment.getText().toString().length() > 0){
total_amount =Integer.parseInt(payment.getText().toString()) ;
}
EditText payment = (EditText) findViewById(R.id.amount);
payment.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
total_amount =Integer.parseInt(s) ;
payment.setText(""+(total_amount/100));
}
});
For that you have to use textWatcher like this : 为此,您必须像这样使用textWatcher:
payment.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
use addTextChangedListener()
使用addTextChangedListener()
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
EditText payment = (EditText) findViewById(R.id.amount);
if (payment.getText().toString().length() > 0){
double total_amount =Double.parseDouble(payment.getText().toString()) ;
String amm = String.valueOf(total_amount/100);
payment.setText(amm);
You are looking for the TextWatcher
in order to alter their input as they enter it. 您正在寻找TextWatcher
,以便在输入时更改其输入。 Here is an example: 这是一个例子:
TextWatcher paymentWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
//Do your calculations here
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
};
payment.addTextChangedListener(paymentWatcher);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.