如何替换活动中的textChangedListener

时间:2012-01-31 03:37:11

标签: java android listener

对于我的Android应用程序,每次textChangedListener切换状态时我都需要更改RadioButton。有4个可能的按钮,每个按钮都有一个相应的EditText框。我目前只实现了4个按钮中的2个,当我从实现按钮(即按钮1)切换到未实现按钮时,没有问题。但是,当我尝试选择另一个已实现的按钮时(即按钮2),应用程序崩溃。

Bellow是我的一些代码,展示了我textChangedListener的实现。

public TextWatcher last;
public TextWatcher _list1;
public TextWatcher _list2;

public EditText active;
public RadioButton checked;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final EditText t1 = (EditText) findViewById(R.id.1text);
    final EditText t2 = (EditText) findViewById(R.id.2text);
    final EditText t3 = (EditText) findViewById(R.id.3text);
    final EditText t4 = (EditText) findViewById(R.id.4text);

    final RadioGroup g1 = (RadioGroup) findViewById(R.id.radioGroup1);
    g1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            checked = (RadioButton)g1.findViewById(g1.getCheckedRadioButtonId());

            if (((String) checked.getText()).equalsIgnoreCase("But1"))
            {
                active.removeTextChangedListener(last);
                active = t1;
                active.setText("");
                active.addTextChangedListener(_list1);
                last = _list1;
            }
            else if (((String) checked.getText()).equalsIgnoreCase("But2"))
            {
                active.removeTextChangedListener(last);
                active = t2;
                active.setText("");
                active.addTextChangedListener(_list2);
                last = _list2;
            }
            else if (((String) checked.getText()).equalsIgnoreCase("But3"))
            {
                active = t3;
            }
            else if (((String) checked.getText()).equalsIgnoreCase("But4"))
            {
                active = t4;
            }
        }
    });

    checked = (RadioButton)g1.findViewById(g1.getCheckedRadioButtonId());

    active = t1;
    active.setText("");
    _list1 = (new TextWatcher(){
        public void afterTextChanged(Editable s) {}
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            %Useful stuff
        }
    });
    _list2 = (new TextWatcher(){
        public void afterTextChanged(Editable s) {}
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            %Different useful stuff
        }
    });
    active.addTextChangedListener(_list1);
    last = _list1;
}

同样,我的问题是,当我尝试从一个TextChangedListener切换到另一个TextChangeListener时,无论每个侦听器中实现了什么,我的应用程序都会崩溃。我觉得有一些非常明显的东西,我忽略了01-31 08:29:17.673: INFO/ActivityManager(59): Displayed activity com.ikiar.engtools/.Number: 659 ms (total 659 ms) 01-31 08:29:24.083: DEBUG/AndroidRuntime(276): Shutting down VM 01-31 08:29:24.083: WARN/dalvikvm(276): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): FATAL EXCEPTION: main 01-31 08:29:24.103: ERROR/AndroidRuntime(276): java.lang.ClassCastException: android.text.SpannableStringBuilder 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at com.ikiar.engtools.Number$3.onTextChanged(Number.java:122) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.TextView.sendOnTextChanged(TextView.java:6131) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.TextView.setText(TextView.java:2691) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.TextView.setText(TextView.java:2556) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.EditText.setText(EditText.java:75) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.TextView.setText(TextView.java:2531) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at com.ikiar.engtools.Number$1.onCheckedChanged(Number.java:71) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.RadioGroup.access$600(RadioGroup.java:52) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:342) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.CompoundButton.setChecked(CompoundButton.java:127) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.CompoundButton.toggle(CompoundButton.java:86) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.RadioButton.toggle(RadioButton.java:69) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.widget.CompoundButton.performClick(CompoundButton.java:98) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.view.View$PerformClick.run(View.java:8816) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.os.Handler.handleCallback(Handler.java:587) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.os.Handler.dispatchMessage(Handler.java:92) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.os.Looper.loop(Looper.java:123) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at android.app.ActivityThread.main(ActivityThread.java:4627) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at java.lang.reflect.Method.invokeNative(Native Method) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at java.lang.reflect.Method.invoke(Method.java:521) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 01-31 08:29:24.103: ERROR/AndroidRuntime(276): at dalvik.system.NativeStart.main(Native Method) 01-31 08:29:24.113: WARN/ActivityManager(59): Force finishing activity com.ikiar.engtools/.Number 01-31 08:29:24.523: INFO/ARMAssembler(59): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x3659b0:0x365a6c] in 365498 ns 01-31 08:29:24.653: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{450232b0 com.ikiar.engtools/.Number} 的特征和实现。任何帮助将不胜感激!

谢谢!

我的错误日志:(注意:活动名称为“数字”)

{{1}}

3 个答案:

答案 0 :(得分:2)

我认为您的布局文件存在一些问题。

我编辑了代码并创建了main.xml,并将EditText id从1text,2text,3text,4text更改为text1,text2,text3,text4

下面给出的代码正在发挥作用。

以下是活动代码

    public TextWatcher last;
    public TextWatcher _list1;
    public TextWatcher _list2;

    public EditText active;
    public RadioButton checked;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final EditText t1 = (EditText) findViewById(R.id.text1);
        final EditText t2 = (EditText) findViewById(R.id.text2);
        final EditText t3 = (EditText) findViewById(R.id.text3);
        final EditText t4 = (EditText) findViewById(R.id.text4);
        final Context c = getApplicationContext();

        final RadioGroup g1 = (RadioGroup) findViewById(R.id.radioGroup1);
        g1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                checked = (RadioButton) g1.findViewById(g1.getCheckedRadioButtonId());

                if (((String) checked.getText()).equalsIgnoreCase("But1")) {
                    active.removeTextChangedListener(last);
                    active = t1;
                    active.setText("");
                    active.addTextChangedListener(_list1);
                    last = _list1;
                } else if (((String) checked.getText()).equalsIgnoreCase("But2")) {
                    active.removeTextChangedListener(last);
                    active = t2;
                    active.setText("");
                    active.addTextChangedListener(_list2);
                    last = _list2;
                } else if (((String) checked.getText()).equalsIgnoreCase("But3")) {
                    active = t3;
                } else if (((String) checked.getText()).equalsIgnoreCase("But4")) {
                    active = t4;
                }
            }
        });

        checked = (RadioButton) g1.findViewById(g1.getCheckedRadioButtonId());

        active = t1;
        active.setText("");
        _list1 = (new TextWatcher() {
            public void afterTextChanged(Editable s) {
            }

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

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Toast.makeText(c, "onTextChanged called(_list1 watcher)", Toast.LENGTH_SHORT).show();
            }
        });
        _list2 = (new TextWatcher() {
            public void afterTextChanged(Editable s) {
            }

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

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Toast.makeText(c, "onTextChanged called(_list2 watcher)", Toast.LENGTH_SHORT).show();
            }
        });
        active.addTextChangedListener(_list1);
        last = _list1;
    }

}


以下是main.xml文件的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/But1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="But1" />

        <EditText
            android:id="@+id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text" />

        <RadioButton
            android:id="@+id/But2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="But2" />

        <EditText
            android:id="@+id/text2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text" />

        <RadioButton
            android:id="@+id/But3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="But3" />

        <EditText
            android:id="@+id/text3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text" />

        <RadioButton
            android:id="@+id/But4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="But4" />

        <EditText
            android:id="@+id/text4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text" >
        </EditText>
    </RadioGroup>

</LinearLayout>

试试这个。它的工作。

答案 1 :(得分:0)

希望这个帮助

如果但是1

    t1.addTextChangedListener(new TextWatcher(){

        public void afterTextChanged(Editable s) {}         

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

        public void onTextChanged(CharSequence s, int start, int before,int count) {
            // TODO Auto-generated method stub
                            do stuff
        }});

否则,如果只是1

    t1.addTextChangedListener(new TextWatcher(){

        public void afterTextChanged(Editable s) {}         

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

        public void onTextChanged(CharSequence s, int start, int before,int count) {
            // TODO Auto-generated method stub
                           another stuff
        }});

答案 2 :(得分:0)

看起来你的EditText在调用方法setText("")时没有任何监听器,因为你在调用这个方法之前就删除了它。那么如何做到这一点,

if (((String) checked.getText()).equalsIgnoreCase("But1"))
            {
                active.setText("");
                active.removeTextChangedListener(last);
                active = t1;

                active.addTextChangedListener(_list1);
                last = _list1;
            }
            else if (((String) checked.getText()).equalsIgnoreCase("But2"))
            {
                active.setText("");

               active.removeTextChangedListener(last);
                active = t2;
                                active.addTextChangedListener(_list2);
                last = _list2;
            }