我不知道如何在 Fragment
中实现 onKeyPreIme(int keyCode, KeyEvent event)
。
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK &&
event.getAction() == KeyEvent.ACTION_UP) {
// do your stuff
return false;
}
return super.dispatchKeyEvent(event);
}
我尝试了很多,但没有任何效果。此外,我在 Google 或 Stack Overflow 上找不到任何内容。我想在按下后退键并且软键盘启动时执行一个操作。在我的 EditText
上设置 onKeyListener
不起作用,因为当软键盘启动时不会调用 KeyEvent.KEYCODE_BACK
。我感谢任何帮助和源代码。
最佳答案
我能够通过对与键盘输入相关的 EditText View 进行子类化来实现 onKeyPreIme。目标是制作一个自定义锁定屏幕,用户必须输入密码或离开应用程序。当用户点击“键盘向下”按钮时,键盘不会消失。
确保为子类化的 EditText 创建一个单独的 .java 文件。此外,请务必在下面的代码中使用构造函数(必须通过 AttrubuteSet)。
我意识到我对 onKeyPreIme 的实现可能与您的不匹配,但它确实演示了如何在 InputMethodManager 执行之前拦截键盘事件。
希望对您有所帮助。
UserLockActivity 截图
EditText 子类
public class LockEditText extends EditText {
/* Must use this constructor in order for the layout files to instantiate the class properly */
public LockEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public boolean onKeyPreIme (int keyCode, KeyEvent event)
{
// Return true if I handle the event:
// In my case i want the keyboard to not be dismissible so i simply return true
// Other people might want to handle the event differently
System.out.println("onKeyPreIme " +event);
return true;
}
}
UserLockActivity.java
public class UserLockActivity extends Activity
{
private LockEditText editText1;
private LockEditText editText2;
private LockEditText editText3;
private LockEditText editText4;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_lock);
editText1 = (LockEditText) findViewById(R.id.lock_text_1);
editText2 = (LockEditText) findViewById(R.id.lock_text_2);
editText3 = (LockEditText) findViewById(R.id.lock_text_3);
editText4 = (LockEditText) findViewById(R.id.lock_text_4);
setupTextChangedListener(editText1);
setupTextChangedListener(editText2);
setupTextChangedListener(editText3);
setupTextChangedListener(editText4);
// A method to bring out the keyboard when the view appears
setFocusOnEditText(editText1);
}
public void setFocusOnEditText(LockEditText editText)
{
editText.clearFocus();
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
public void setupTextChangedListener(LockEditText editText)
{
editText.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
@Override
public void afterTextChanged(Editable arg0)
{
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,int arg2, int arg3)
{
// TODO Auto-generated method stub
}
});
}
}
activity_user_lock.xml布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".UserLockActivity" >
<TextView
android:id="@+id/main_lock_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:paddingTop="60dp"
android:paddingBottom="20dp"
android:text="@string/enter_passcode"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:id="@+id/lock_input_layout"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="@+id/main_lock_text"
android:orientation="horizontal" >
<com.yourpackage.yourappname.LockEditText
android:id="@+id/lock_text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:ems="10"
android:inputType="numberPassword"
android:textSize="30sp"
android:gravity="center_horizontal"
android:textStyle="bold" >
</com.yourpackage.yourappname.LockEditText>
<com.yourpackage.yourappname.LockEditText
android:id="@+id/lock_text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:ems="10"
android:inputType="numberPassword"
android:textSize="30sp"
android:gravity="center_horizontal"
android:textStyle="bold" >
</com.yourpackage.yourappname.LockEditText>
<com.yourpackage.yourappname.LockEditText
android:id="@+id/lock_text_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:ems="10"
android:inputType="numberPassword"
android:textSize="30sp"
android:gravity="center_horizontal"
android:textStyle="bold">
</com.yourpackage.yourappname.LockEditText>
<com.yourpackage.yourappname.LockEditText
android:id="@+id/lock_text_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:ems="10"
android:inputType="numberPassword"
android:textSize="30sp"
android:gravity="center_horizontal"
android:textStyle="bold" >
</com.yourpackage.yourappname.LockEditText>
</LinearLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/lock_input_layout"
android:layout_centerHorizontal="true"
android:text="text" />
</RelativeLayout>
关于android - 在 Fragment 中实现 onKeyPreIme(int keyCode, KeyEvent event),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13591012/