我的申请中发生了一个尴尬的情况。
我的应用程序具有价格EditText
等功能,点击它会选择所有文本,然后您可以输入价格。当用户点击EditText
时,它正在选择所有文本,但是当用户输入任何文本时,将不会显示第一个数字。然后将正确显示另一个数字。我添加了TextChangeListner
并尝试弄清楚但是当用户输入第一个字符时,我得到 0 CharSequence
。
我有EditText
的inputfilter。检查以下代码是否为editfilter。
public static class InputFilterForDoubleMinMax implements InputFilter {
private double min, max;
public InputFilterForDoubleMinMax(double min, double max) {
this.min = min;
this.max = max;
}
public InputFilterForDoubleMinMax(String min, String max) {
this.min = Double.parseDouble(min);
this.max = Double.parseDouble(max);
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
try {
String temp = dest.toString() + source.toString();
if (source.toString().equalsIgnoreCase(".")) {
return "";
} else if (temp.toString().indexOf(",") != -1) {
temp = temp.toString().substring(temp.toString().indexOf(",") + 1);
if (temp.length() > 2) {
return "";
}
}
double input = Double.parseDouble(dest.toString().replace(",", ".").replace("€", "") + source.toString().replace(",", ".").replace("€", ""));
if (isInRange(min, max, input))
return null;
} catch (NumberFormatException nfe) {
}
return "";
}
private boolean isInRange(double a, double b, double c) {
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
}
任何帮助表示感谢。
答案 0 :(得分:2)
问题已解决。问题是由于输入过滤器内部实现了逻辑。
以下是我更新的代码输入过滤器。
public static class InputFilterForDoubleMinMax implements InputFilter {
private double min, max;
public InputFilterForDoubleMinMax(double min, double max) {
this.min = min;
this.max = max;
}
public InputFilterForDoubleMinMax(String min, String max) {
this.min = Double.parseDouble(min);
this.max = Double.parseDouble(max);
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
try {
String temp;
if (dstart == 0) {
temp = source.toString();
} else {
temp = dest.toString() + source.toString();
}
if (source.toString().equalsIgnoreCase(".")) {
return "";
} else if (temp.toString().indexOf(",") != -1) {
temp = temp.toString().substring(temp.toString().indexOf(",") + 1);
if (temp.length() > 2) {
return "";
}
}
double input;
if (dstart == 0) {
input = Double.parseDouble(source.toString().replace(",", ".").replace("€", ""));
} else {
input = Double.parseDouble(dest.toString().replace(",", ".").replace("€", "") + source.toString().replace(",", ".").replace("€", ""));
}
if (isInRange(min, max, input))
return null;
} catch (NumberFormatException nfe) {
}
return "";
}
private boolean isInRange(double a, double b, double c) {
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
}
答案 1 :(得分:0)
这很好。
package com.imrhk.testapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText = (EditText) findViewById(R.id.edit_text);
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!TextUtils.isEmpty(editText.getText().toString())) {
editText.selectAll();
}
}
});
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(editText.getSelectionStart() != -1) {
//editText.setText("");
editText.setSelected(false);
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"/>
</LinearLayout>
编辑:
将其添加为过滤器
editText.setFilters(new InputFilter[]{new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if(String.valueOf(source).equals(".") && String.valueOf(dest).equals("100")) //replace it with maxed value
return "";
try {
Float f = Float.parseFloat(String.valueOf(dest) + String.valueOf(source.toString()));
if (f < 0.0f || f > 100.0f) //replace it with max/min value
return "";
return null;
} catch (NumberFormatException e) {
return null;
}
}
}});