android - 无法解析(或导入)Android 小部件 OnEditorActionListener

标签 android android-studio android-edittext listener actionlistener

我是编码新手,在尝试使用 OnEditorActionListener 帮助执行操作时,一旦用户将数据输入到 EditText 并按下软键盘上的“Go”,我就会不断出错。我已经搜索过,大多数提供的解决方案都假设 OnEditorActionListener 已经导入。

用作生成我自己的代码的指南的文章:

https://developer.android.com/training/keyboard-input/style.html

https://github.com/codepath/android_guides/wiki/Basic-Event-Listeners

我的 XML 代码:

<EditText
        android:id="@+id/editTextCurrentBalance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:imeOptions="actionGo"
        android:inputType="numberDecimal"
        android:singleLine="true">
        <requestFocus />
</EditText>

我的 Java 代码( fragment ):

import android.widget.TextView.OnEditorActionListener; 

EditText editTextListener = (EditText) findViewById(R.id.editTextCurrentBalance);
editTextListener.setOnEditorActionListener(new OnEditorActionListener(){...});

第一个错误:“import android.widget.TextView.OnEditorActionListener;”给我一个错误,提示“未使用的导入语句”并且整行代码显示为灰色。

第二个错误:“无法解析符号‘setOnEditorActionListener’”

修复尝试:当我按下 CTRL + I 时,我收到一条消息“未找到要实现的方法”。

感谢任何帮助!

更新:我的 OnEditorActionListener Java 代码在 OnCreate 方法括号之外。一旦放入其中,错误就清除了。

最佳答案

您好,欢迎来到 SO

第一个错误:嗯,这实际上不是错误,它只是您的 IDE 给您的警告,意味着您导入了一个您未使用的类。最好删除任何未使用的导入(Android Studio 中的 Ctrl + Alt + O)

第二个错误:我相信会弹出这个错误,因为您还没有导入 EditText 类,但我不确定

总之,先来个战利品吧

//We are import the classes you need here
import android.widget.EditText;
import android.widget.TextView;

//here is just the onCreate method from an Activity, 
//I have left out most of the boilerplate code

public class MainActivity extends AppCompatActivity {
    //make sure you are placing the code in onCreate method, or
    //a method called from onCreate, or any method other life cycle 
    //method that suits yours needs
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        /*snip*/

        //grabing the EditText object by it's ID you defined in the layout file
        //I have renamed the object to "editText" because "listener" 
        //suffix made no sense here, it's an EditText class you are 
        //crating, which is not a listener 
        EditText editText = (EditText) findViewById(R.id.editTextCurrentBalance);

        //here we are creating a new anonymous class and setting to
        //trigger when an "Editor Action" happens on editTextListener
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                //do something here
                return false;
            }
        });
    }

如果我们不打算在其他地方重用该类,匿名类只是创建新类的一种更简单的方法。它特定于您当前的需求

希望这对您有所帮助 :) 祝您学习 Android 和 Java 顺利

关于android - 无法解析(或导入)Android 小部件 OnEditorActionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38686756/

相关文章:

android - 创建上面有 textview 的 imageview 图像

android - ImageView 不显示某些图像

android - 如何在 Android View 之间添加 GLSurfaceView?

android - 包 org.apache.cordova 不存在 android studio

android - 出现错误 : "Failed to resolve: localbroadcastmanager" in Connecting Android project to Firebase

android - 了解android编程的xml文件

android - 如何使用 EditText 在 Android 中检索字符串?

android - 显示对话框后或 EditText 获得焦点时,隐藏的导航栏(带有主页、后退和概览按钮的底部栏)可见

android - 在 Android 应用程序中使用大型数据库

android - 对包含两种数据类的 ArrayList<Any> 进行排序