gpt4 book ai didi

java - 如何替换 Activity 中的 textChangedListener

转载 作者:行者123 更新时间:2023-11-29 22:13:21 25 4
gpt4 key购买 nike

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

下面是我的一些代码,显示了我的 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 的特性和实现,我觉得有一些非常明显的东西被我忽略了。任何帮助将不胜感激!

谢谢!

我的错误日志:(注意: Activity 名称是“Number”)

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}

最佳答案

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

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

下面给出的代码有效。

Activity 代码如下

    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>

试试这个。它的工作。

关于java - 如何替换 Activity 中的 textChangedListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9074045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com