无法解析(或导入)Android 小部件 OnEditorActionListener
Cannot resolve (or import) Android widget OnEditorActionListener
我是编码新手,在尝试使用 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 代码(片段):
import android.widget.TextView.OnEditorActionListener;
EditText editTextListener = (EditText) findViewById(R.id.editTextCurrentBalance);
editTextListener.setOnEditorActionListener(new OnEditorActionListener(){...});
第一个错误:“import android.widget.TextView.OnEditorActionListener;”给我一个错误 "Unused Import Statement" 并且整行代码都变灰了。
第二个错误:“无法解析符号 'setOnEditorActionListener'”
修复尝试: 当我按 CTRL + I 时,我收到一条消息 "No methods to implement have been found"。
感谢任何帮助!
更新: 我的 Java OnEditorActionListener 代码在 OnCreate 方法括号之外。一旦放入其中,错误就清除了。
您好,欢迎来到 SO
第一个错误:嗯,这实际上不是错误,它只是一个警告,你的 IDE 给你的意思是你导入了一个 class没有使用。最好删除任何未使用的导入(Android Studio 中的 Ctrl + Alt + O)
第二个错误:我相信这个错误会弹出,因为你还没有导入 EditText class,但我不确定
总之,就拿这个
//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;
}
});
}
匿名 classes 只是创建新 class 的一种更简单的方法,如果我们不打算在其他地方重用那个 class 即。它特定于您当前的需求
希望这对您有所帮助 :) 祝您学习 Android 和 Java
我是编码新手,在尝试使用 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 代码(片段):
import android.widget.TextView.OnEditorActionListener;
EditText editTextListener = (EditText) findViewById(R.id.editTextCurrentBalance);
editTextListener.setOnEditorActionListener(new OnEditorActionListener(){...});
第一个错误:“import android.widget.TextView.OnEditorActionListener;”给我一个错误 "Unused Import Statement" 并且整行代码都变灰了。
第二个错误:“无法解析符号 'setOnEditorActionListener'”
修复尝试: 当我按 CTRL + I 时,我收到一条消息 "No methods to implement have been found"。
感谢任何帮助!
更新: 我的 Java OnEditorActionListener 代码在 OnCreate 方法括号之外。一旦放入其中,错误就清除了。
您好,欢迎来到 SO
第一个错误:嗯,这实际上不是错误,它只是一个警告,你的 IDE 给你的意思是你导入了一个 class没有使用。最好删除任何未使用的导入(Android Studio 中的 Ctrl + Alt + O)
第二个错误:我相信这个错误会弹出,因为你还没有导入 EditText class,但我不确定
总之,就拿这个
//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;
}
});
}
匿名 classes 只是创建新 class 的一种更简单的方法,如果我们不打算在其他地方重用那个 class 即。它特定于您当前的需求
希望这对您有所帮助 :) 祝您学习 Android 和 Java