【问题标题】:Catch "DONE" key press from soft keyboard [closed]从软键盘捕捉“DONE”按键[关闭]
【发布时间】:2013-01-09 13:40:12
【问题描述】:

我正在寻找可以从软键盘捕获“完成”按钮的按键的代码。当按下完成按钮时,我需要将按钮状态更改为启用,然后用户可以继续下一个活动。

我在 stackoverflow 上找到了这段代码,但我无法在没有错误的情况下实现它。你能帮帮我吗?

editText = (EditText) findViewById(R.id.edit_text);

editText.setOnEditorActionListener(new OnEditorActionListener() {
   @Override
   public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
       if (actionId == EditorInfo.IME_ACTION_DONE) {
           // do your stuff here
       }
       return false;
}
});

这是整个 .java 文件

package com.example.start201;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class FirstActivity extends Activity {

   private EditText editText;

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.layout_firstactivity);

       editText = (EditText) findViewById(R.id.editText2);

       editText.setOnEditorActionListener(new OnEditorActionListener() {
           @Override
           public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
               if (actionId == EditorInfo.IME_ACTION_DONE) {
                   // do your stuff here
               }
               return false;
           }
       });

}


}

【问题讨论】:

  • 你有什么错误?
  • 你认为错误只是随机的和无用的吗?为什么不告诉我们您遇到了什么错误?
  • 我收到这些错误:此行有多个标记 - TextView 类型中的方法 setOnEditorActionListener(TextView.OnEditorActionListener) 不适用于参数(新 OnEditorActionListener(){}) - 无法解析 OnEditorActionListener到一个类型
  • 您是否导入了OnEditorActionListener?另见stackoverflow.com/questions/2004344/… 和stackoverflow.com/questions/5077425/…
  • 定义“错误”。修复你的导入。

标签: java android


【解决方案1】:

我把它整理好了,这段代码运行流畅,它可以识别软键盘上的 DONE 按钮的按键。

布局文件:

   <EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:imeOptions="actionDone"
    android:inputType="numberDecimal" />

   <Button
    android:id="@+id/button1"
    android:layout_width="80dp"
    android:layout_height="30dp"
    android:text="NEXT"
    android:textSize="10sp" />

</RelativeLayout>

.java 文件:

package com.example.drywallcalculator102v;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class FirstActivity extends Activity {

   private EditText editText;
   private Button btnNext;

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

       btnNext = (Button) findViewById(R.id.button1);
       btnNext.setEnabled(false);

       editText = (EditText) findViewById(R.id.editText2);

       editText.setOnEditorActionListener(new OnEditorActionListener() {

           @Override
           public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
               // TODO Auto-generated method stub
               if (actionId == EditorInfo.IME_ACTION_DONE) {
                   // do your stuff here
                   btnNext.setEnabled(true);
               }
               return false;
           }
       });

   }


}

【讨论】:

    【解决方案2】:

    尝试添加此导入:

    import android.widget.TextView.OnEditorActionListener;
    

    【讨论】:

      猜你喜欢
      • 2011-03-03
      • 1970-01-01
      • 2012-09-30
      • 1970-01-01
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 2013-05-15
      • 1970-01-01
      相关资源
      最近更新 更多