gpt4 book ai didi

java - 从软键盘捕获 "DONE"按键

转载 作者:行者123 更新时间:2023-12-01 06:40:02 28 4
gpt4 key购买 nike

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

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

}


}

最佳答案

我已经解决了,这段代码运行顺利,它可以识别软键盘上“完成”按钮的按键。

布局文件:

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

}


}

关于java - 从软键盘捕获 "DONE"按键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14524393/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com