简体   繁体   English

编辑文本的变量改变了监听器

[英]variable on edit text changed listener

I need help with this code, it's not working. 我需要这个代码的帮助,它不起作用。 I would like to change boolean value to true when the text changed. 我希望在文本更改时将boolean值更改为true Any help will be appreciated. 任何帮助将不胜感激。

public class Atiras {

    public void atirasfigyelo(final EditText valtozo,Cursor sql,String SQLoszlop,final boolean ell){

        valtozo.setText("" + sql.getInt(sql.getColumnIndex(SQLoszlop)));

        ell=false;
        valtozo.addTextChangedListener(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) {
                ell=true;
            }

            @Override
            public void afterTextChanged(Editable s) {
                valtozo.setBackgroundColor(Color.RED);
            }

        });
    }
}

This code won't work because in your function: 此代码无效,因为在您的函数中:
public void atirasfigyelo(final EditText valtozo,Cursor sql,String SQLoszlop,final boolean ell){

you are making ell as final boolean ell final variable. 你将ell作为final boolean ell final变量。

You can create a class level variable that you can change from inside annonimus class like below: 您可以创建一个类级别变量,您可以从annonimus类内部进行更改,如下所示:

public class Atiras {
    private boolean isTrue;
    public void atirasfigyelo(final EditText valtozo, Cursor sql, String SQLoszlop, final boolean ell) {    
        valtozo.setText("" + sql.getInt(sql.getColumnIndex(SQLoszlop)));

        isTrue= false;
        valtozo.addTextChangedListener(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) {    
                isTrue= true;    
            }

            @Override
            public void afterTextChanged(Editable s) {
                valtozo.setBackgroundColor(Color.RED);    
            }    
        });    
    }    
}

It will work because 6.3. 因为6.3 ,它将起作用 Scope of a Declaration 宣言的范围

The scope of a local variable declaration in a block (§14.4) is the rest of the block in which the declaration appears, starting with its own initializer and including any further declarators to the right in the local variable declaration statement. 块中的局部变量声明的范围(第14.4节)是该声明在其中出现的其余部分,从其自身的初始化程序开始,并在该局部变量声明语句的右侧包括其他任何声明符。

You try to create a JavaBean or POJO instead of this argument list. 您尝试创建JavaBean或POJO而不是此参数列表。 Then you can change the 'ell' variable. 然后你可以改变'ell'变量。 And the Cursor object must be to close. 并且Cursor对象必须关闭。 First you do the query then convert the record to object. 首先,您执行查询,然后将记录转换为对象。 Then you make the object what you want. 然后,您可以根据需要设置对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM