【安卓】关于输入框的一个异常

W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection

我出现这种问题的情况是:

对一个输入框添加了输入监听,但是在监听中又对这个输入框进行了一次赋值,导致循环赋值,最终造成应用卡死。

错误代码:

appCompatEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                
                double a = 1;
                double b = 2;
                appCompatEditText.setText((Math.min(a, b))+"");

            }

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

            }

            @Override
            public void afterTextChanged(Editable s) {

            }

        });

修改后:

double a = 1;
double b = 2;
appCompatEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                
                if(a<b){
                    appCompatEditText.setText(a+"");
                    a = 0;
                    b = 0;
                }
                

            }

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

            }

            @Override
            public void afterTextChanged(Editable s) {

            }

        });

总结:在输入监听中,不要没有判断条件的对此输入框赋值

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的 Android 数值转化页面的代码,包括两个 EditText 输入框和两个 Button 按钮,可以将输入的数字进行摄氏度和华氏度之间的转换: 1. 布局文件 activity_main.xml: ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/editTextCelsius" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/celsius"/> <EditText android:id="@+id/editTextFahrenheit" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/fahrenheit"/> <Button android:id="@+id/buttonCelsiusToFahrenheit" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/celsius_to_fahrenheit"/> <Button android:id="@+id/buttonFahrenheitToCelsius" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/fahrenheit_to_celsius"/> </LinearLayout> ``` 2. 字符串资源文件 strings.xml: ```xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Number Converter</string> <string name="celsius">Celsius</string> <string name="fahrenheit">Fahrenheit</string> <string name="celsius_to_fahrenheit">Convert Celsius to Fahrenheit</string> <string name="fahrenheit_to_celsius">Convert Fahrenheit to Celsius</string> </resources> ``` 3. Java 代码 MainActivity.java: ```java import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.widget.Button; import android.widget.EditText; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private EditText editTextCelsius, editTextFahrenheit; private Button buttonCelsiusToFahrenheit, buttonFahrenheitToCelsius; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextCelsius = findViewById(R.id.editTextCelsius); editTextFahrenheit = findViewById(R.id.editTextFahrenheit); buttonCelsiusToFahrenheit = findViewById(R.id.buttonCelsiusToFahrenheit); buttonFahrenheitToCelsius = findViewById(R.id.buttonFahrenheitToCelsius); editTextCelsius.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) { if (s.length() > 0) { double celsius = Double.parseDouble(s.toString()); double fahrenheit = celsiusToFahrenheit(celsius); editTextFahrenheit.setText(String.valueOf(fahrenheit)); } else { editTextFahrenheit.setText(""); } } @Override public void afterTextChanged(Editable s) { } }); editTextFahrenheit.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) { if (s.length() > 0) { double fahrenheit = Double.parseDouble(s.toString()); double celsius = fahrenheitToCelsius(fahrenheit); editTextCelsius.setText(String.valueOf(celsius)); } else { editTextCelsius.setText(""); } } @Override public void afterTextChanged(Editable s) { } }); buttonCelsiusToFahrenheit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (editTextCelsius.getText().length() > 0) { double celsius = Double.parseDouble(editTextCelsius.getText().toString()); double fahrenheit = celsiusToFahrenheit(celsius); editTextFahrenheit.setText(String.valueOf(fahrenheit)); } } }); buttonFahrenheitToCelsius.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (editTextFahrenheit.getText().length() > 0) { double fahrenheit = Double.parseDouble(editTextFahrenheit.getText().toString()); double celsius = fahrenheitToCelsius(fahrenheit); editTextCelsius.setText(String.valueOf(celsius)); } } }); } private double celsiusToFahrenheit(double celsius) { return celsius * 1.8 + 32; } private double fahrenheitToCelsius(double fahrenheit) { return (fahrenheit - 32) / 1.8; } } ``` 这个页面实现了一个简单的摄氏度和华氏度之间的转换功能,通过输入摄氏度或华氏度,或者点击对应的按钮,即可进行转换。需要注意的是,为了避免用户输入非法字符导致程序崩溃,需要进行异常处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WWGtest

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值