crosswalk使用--AlertDialog.Builder弹窗--返回键弹窗、prompt弹窗
1.遗留问题:弹出窗口关闭后整个屏幕亮度非常高,一段时间后才会恢复。
2.遇到的问题:使用 import android.support.v7.app.AlertDialog 弹窗就闪退。
返回键 |
通过重写dispatchKeyEvent函数修改返回键事件
package com.example.lyj.crosswalk; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.KeyEvent; import org.xwalk.core.XWalkView; public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AndroidBug5497Workaround.assistActivity(this); XWalkView xWalkView = findViewById(R.id.web); xWalkView.loadUrl("file:///android_asset/index.html"); } @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && 0 == event.getRepeatCount() && event.getAction() == KeyEvent.ACTION_DOWN){ new AlertDialog.Builder(MainActivity.this) .setTitle("退出确定") .setMessage("是否确定退出?") .setPositiveButton("确定退出", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { finish(); } }) .setNegativeButton("取消退出", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }) .create() .show(); return false; } return super.dispatchKeyEvent(event); } }
|
prompt监听 |
重写onJsPrompt函数,自定义弹出窗口样式和内容xWalkView.setUIClient(new XWalkUIClient(xWalkView){ @Override public boolean onJsPrompt(XWalkView view, String url, String message, final String defaultValue, final XWalkJavascriptResult result) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); final EditText editText = new EditText(MainActivity.this); Uri uri = Uri.parse(message); editText.setText(defaultValue); if(uri.getAuthority().equals("serverAddr")) { builder.setTitle("请输入服务器地址").setView(editText); } else if(uri.getAuthority().equals("operator")) { builder.setTitle("请输入运营商信息").setView(editText); } else { builder.setTitle("error"); } builder.setPositiveButton("输入确认", new DialogInterface.OnClickListener() { @Override //确认键处理 public void onClick(DialogInterface dialogInterface, int i) { result.confirmWithResult(editText.getText().toString()); } }); builder.setNegativeButton("取消输入", new DialogInterface.OnClickListener() { @Override //取消键处理 public void onClick(DialogInterface dialogInterface, int i) { result.confirmWithResult(defaultValue); } }); builder.setOnKeyListener(new DialogInterface.OnKeyListener() { @Override //返回键处理 public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) { if(KeyEvent.KEYCODE_BACK == i) return true; //返回键拦截 return false; //其他键直接返回 } }); builder.show(); return true; } });
|