gpt4 book ai didi

java - 如何在输入法服务中制作AlertDialog View ?

转载 作者:行者123 更新时间:2023-11-30 01:53:58 25 4
gpt4 key购买 nike

我想制作一个仅用于软键盘的输入法。我如何在输入法中弹出onkey事件。

我正在创建 Dialog,但您看到我的 logcat 时出现问题:

09-14 11:16:54.349: E/MessageQueue-JNI(7172):   at android.inputmethodservice.KeyboardView.detectAndSendKey(KeyboardView.java:824)

软键盘.java

这里是java代码

public void onKey(int primaryCode, int[] keyCodes) {
if (primaryCode == -2) {

// add this to your code
dialog = builder.create();
Window window = dialog.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.token = mInputView.getWindowToken();
lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
window.setAttributes(lp);
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
// end addons
builder.show();
}

感谢提前..

最佳答案

您需要拥有ACTION_MANAGE_OVERLAY_PERMISSION权限才能在输入法中打开/显示Alert onkey事件。

在您设置自定义键盘之前检查覆盖权限。

 final boolean overlayEnabled = Settings.canDrawOverlays(MainActivity.this);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !overlayEnabled) {
openOverlaySettings();
}



@TargetApi(Build.VERSION_CODES.M)
private void openOverlaySettings() {
final Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
try {
startActivityForResult(intent, RC_OVERLAY);
} catch (ActivityNotFoundException e) {
Log.e("MainActivity", e.getMessage());
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case RC_OVERLAY:
final boolean overlayEnabled = Settings.canDrawOverlays(this);
if (overlayEnabled) {
preferenceManager.setBooleanPref(IS_CYBER_BULLING_ON, true);
Intent intent = new Intent(MainActivity.this, ImePreferences.class);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
} else {
// switchCyberBulling.setChecked(false);
}
// Do something...
break;
}
}

然后在您的 SoftKeyboard.java 类中,放置警报对话框的代码并将警报类型设置为“TYPE_APPLICATION_OVERLAY”。

AlertDialog.Builder builder = new AlertDialog.Builder(this)
//set icon
.setIcon(android.R.drawable.ic_dialog_alert)

//set title
.setTitle("Warning!")
//set message
.setMessage("This is alert dialog!")
//set positive button
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//set what would happen when positive button is clicked
dialogInterface.dismiss();
}
})
//set negative button
.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//set what should happen when negative button is clicked
dialogInterface.dismiss();
}
});
AlertDialog alertDialog = builder.create();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
}else{
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
alertDialog.show();

不要忘记 Draw Overlay Permission。希望这对你有帮助。 :)

关于java - 如何在输入法服务中制作AlertDialog View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32558153/

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