0

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

我在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;
           }
       });

}


}
4

2 回答 2

12

我把它整理好了,这段代码运行顺利,它可以识别软键盘上的 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;
           }
       });

   }


}
于 2013-01-25T23:32:00.227 回答
-1

尝试添加此导入:

import android.widget.TextView.OnEditorActionListener;
于 2013-01-25T15:13:45.733 回答