标签: textwatcher

Application get crash when adding decimal instead of number into edittext in Android

I'm creating a calculator type app which returns calculations to a bunch of EditText fields with the TextWatcher function. The problem I'm having is that when I type a decimal before a number, my app crashes. Here's what I have so far.

public class MainActivity extends ActionBarActivity {

    EditText editText1;
    EditText editText2;
    EditText editText4;
    EditText editText3;
    EditText editText5;
    EditText editText6;
    EditText editText7;
    EditText editText8;
    TextView textViewtotal;
    TextView textViewnet;
    TextView textViewproceeds;
    TextView textViewprof;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); …
Run Code Online (Sandbox Code Playgroud)

android decimal textwatcher

1
推荐指数
1
解决办法
1085
查看次数

TextWatcher的onTextChanged方法中的count参数对于输入类型为textWebPassword的EditText无法正常工作

TextWatcher的onTextChanged方法中的count参数对于输入类型为textWebPassword的EditText无法正常工作。即使EditText中有超过1个字符,else if(count == 1)中的代码也正在运行。

public class Test extends AppCompatActivity {
private EditText ePassword;
private TextView tPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_in);
    tPassword = (TextView) findViewById(R.id.textView_password);
    ePassword = (EditText) findViewById(R.id.editText_password);
    ePassword.addTextChangedListener(textWatcherPassword);


}
private TextWatcher textWatcherPassword = 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 (count == 0) {
            // start fade out animation
            tPassword.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out));
            //Make the …
Run Code Online (Sandbox Code Playgroud)

android textwatcher android-edittext android-inputtype

1
推荐指数
2
解决办法
2649
查看次数

在Android中动态测量edittext字符串的长度

键入时如何测量在edittext中输入的字符串的长度。因为我想在输入的文本长度超过100个字符时显示警告。提前致谢。

android charactercount textwatcher android-edittext

0
推荐指数
1
解决办法
2558
查看次数

没有无限循环将TextWatcher类附加到多个EditText

我正在尝试根据以下任何输入(每小时,每天,每周,每月,每年)计算此人收到的薪水。输入其中一个时,应自动重新计算其他一个。

这是我的操作方法:

首先,Double在活动顶部定义了5个类型变量。它们是:每小时,每天,每周,每月,每年。然后,我有5个EditText字段,对应于这些变量。我已经附加了一个实现TextWatcher这5个EditText 的自定义子类。

例如:

etHourly = (EditText) findViewById(R.id.etHourly);
etHourly.addTextChangedListener(new EditTextWatcher(etHourly));
Run Code Online (Sandbox Code Playgroud)

这个自定义类具有一个构造函数,该构造函数接受并存储传递给它的视图,因为TextWatcher该类的默认方法无法提供找出哪个View调用了更改的方法。

在将传递的视图保存为自定义子类中的局部变量之后,我afterTextChanged在该子类中继续实现,并获取传递的EditText的值,并将其另存为Double活动顶部的相应定义变量。(例如,如果传递的EditText用于每周薪水,则将该EditText的值设置为weekly变量的双精度值。

最后,在该afterTextChanged方法结束之前,我调用了另一个自定义方法Recalculate(),该方法具有一堆,if()用于检查是否设置了每小时,每天,每周,每月或每年,是否设置setText()了剩余的EditText。问题在于,这setText()将为每个EditText调用TextWatchers,从而导致无限循环。

我该如何克服呢?

这里有一些代码可以更好地理解这一点。在onCreate之前:

Double hourly, daily, weekly, monthly, yearly = 0.0;
EditText etHourly, etDaily, etWeekly, etMonthly, etYearly;
Run Code Online (Sandbox Code Playgroud)

在onCreate()内部:

etHourly = (EditText) findViewById(R.id.etHourly);
etDaily = (EditText) findViewById(R.id.etDaily);
etWeekly = (EditText) findViewById(R.id.etWeekly);
etMonthly = (EditText) findViewById(R.id.etMonthly);
etYearly = (EditText) findViewById(R.id.etYearly);

etHourly.addTextChangedListener(new EditTextWatcher(etHourly));
etDaily.addTextChangedListener(new EditTextWatcher(etDaily));
etWeekly.addTextChangedListener(new EditTextWatcher(etWeekly));
etMonthly.addTextChangedListener(new …
Run Code Online (Sandbox Code Playgroud)

android textwatcher

0
推荐指数
1
解决办法
2688
查看次数

当 EditText 中只有一个字符时防止退格

全部 - 对不起,我的标题太长了,我找不到任何其他方式来表达。所以,我有一个EditText,当只剩下一个字符时(我可以通过 a 监视它TextWatcher)我想禁用退格键。这是我到目前为止所拥有的:

    editText$.addTextChangedListener(new TextWatcher() {
    private String current = "";
    String one = "1";   
    String empty = one; //Right now only set to "1" but want to set to "[any character]"
    public void onTextChanged(CharSequence s, int start, int before, int count) {       
    }
    public void afterTextChanged(Editable s) {

        if (s.toString().equals(empty)) {
            editText$.setOnKeyListener(new OnKeyListener() {                 
                public boolean onKey(View v, int keyCode, KeyEvent event) {                 
                     if(keyCode == KeyEvent.KEYCODE_DEL){                        
                           return true; //Disable backspace key here
                         } …
Run Code Online (Sandbox Code Playgroud)

java string android ime textwatcher

0
推荐指数
1
解决办法
1613
查看次数

EditText中的NumberFormatException

我有一个TextView计算两个EditText.只要一个数字在EditText中,它就可以正常工作,但是一旦删除所有数字,我就会收到此错误

java.lang.NumberFormatException:无法解析''作为整数

我明白为什么我得到错误但我无法弄清楚如何解决它.我在谷歌搜索并搜索了这个网站的答案,但它们似乎不适用于我的情况.我试图捕获NumberFormatException,但我不能这样做.有帮助吗?

items = (EditText)findViewById(R.id.items);
itemcost = (EditText)findViewById(R.id.itemcost);
inventoryvalue = (TextView)findViewById(R.id.inventoryvalue);


TextWatcher textWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
calculateResult();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
};

items.addTextChangedListener(textWatcher);
itemcost.addTextChangedListener(textWatcher);
}

private void calculateResult() throws NumberFormatException {

String s1 = items.getText().toString();
    String s2 = itemcost.getText().toString();
    int value1 = Integer.parseInt(s1);
    int value2 = Integer.parseInt(s2);
    int result = value1 * value2; {

// Calculates …
Run Code Online (Sandbox Code Playgroud)

string android textwatcher numberformatexception

0
推荐指数
1
解决办法
304
查看次数

AutoCompleteTextView 中不出现下拉菜单

我正在开发一个项目,在该项目中我必须访问 SQLite3 数据库中的数据并在键入时向用户显示建议。我已经用 AutoCompleteTextView 尝试过这个,但它不起作用。我正在处理此代码:

ArrayList<String> sugestion = new ArrayList<String>();


public ViewGroup onCreateView (LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState )
{
myAutoComplete = (AutoCompleteTextView) group.findViewById(R.id.searchit);

searchDB = new searchAdapter(getActivity());

searchDB.getWritableDatabase();

myAutoComplete.addTextChangedListener(new TextWatcher(){
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            suggestion = searchDB.getSuggestion(s.toString());

            Collections.sort(suggestion);

            String[] item = new String[suggestion.size()];

            item = suggestion.toArray(item);

            for(String word : item)
                System.out.println(word);

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {                

        }

        @Override
        public void …
Run Code Online (Sandbox Code Playgroud)

java android autocompletetextview android-arrayadapter textwatcher

0
推荐指数
1
解决办法
5485
查看次数

如何在java中匹配正则表达式

我想要一个这样的模式:GJ-16-RS-1234我已经应用了以下模式,但它们不起作用.

我的正则表达式是:

String str_tempPattern = "(^[A-Z]{2})\\-([0-9]{2})\\-([A-Z]{1,2})\\-([0-9]{1,4}$)";

String str_tempPattern = "(^[A-Z]{2})-([0-9]{1,2})-([A-Z]{1,2})-([0-9]{1,4})$";

String str_tempPattern = "^[A-Z]{2}\\-[0-9]{1,2}\\-[A-Z]{1,2}\\-[0-9]{1,4}$";
Run Code Online (Sandbox Code Playgroud)

我正在使用文本观察器检查是否有任何变化 aftertextchange()

Pattern p = Pattern.compile(str_tempPattern, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
    Matcher m = p.matcher(s);

    if (m.find()){

    }
Run Code Online (Sandbox Code Playgroud)

java regex android textwatcher

-2
推荐指数
1
解决办法
5603
查看次数

如何确定是否有人在editText上键入

我正在使用android studio设计聊天应用程序,并且使用fire-base作为云服务提供商,并且我掌握了如何使监听程序(如用户在编辑文本字段中键入内容)的库存。 base将变为true,并且当用户不键入时,该值将变为false。我正在寻找此解决方案大约一个星期,但没有找到关于我的研究的任何答案。

如何放置TextWatcher?

编辑

       final Firebase firebase = new Firebase("https://my-first-sample-is.firebaseio.com/");


            editText = (EditText) findViewById(R.id.editText);

            listView = (ListView) findViewById(R.id.listView);

            listView.setAdapter(new MessageAdapter(this, Message.class, R.layout.fragment_message, firebase.child("chat")));
            editText.setAdapter(new RoomTypeAdapter(this, RoomType.class, R.layout.fragment_message1, firebase.child("Room-Typing")));

            button = (Button) findViewById(R.id.button);



            button.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View view) {
  Message message = new Message();

                message.setMessage(editText.getText().toString());
                editText.setText("");
                message.setAuthor("Name");
                message.setNumMessages(numMessages);


                firebase.child("chat").push().setValue(message);


            }
        });
        editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {

                    return true;
                }
                return false;
            }
        }); …
Run Code Online (Sandbox Code Playgroud)

java android textwatcher

-2
推荐指数
1
解决办法
2170
查看次数