从软键盘上按“DONE”键

时间:2013-01-25 14:59:15

标签: java android

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

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

}


}

2 个答案:

答案 0 :(得分: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;
           }
       });

   }


}

答案 1 :(得分:-1)

尝试添加此导入:

import android.widget.TextView.OnEditorActionListener;