如何在android中的webview中在屏幕上显示弹出窗口?

时间:2014-12-18 13:09:42

标签: android webview popupwindow

我现在正在使用webview,我正努力在webview上显示弹出窗口。每当我在触摸的extact相同位置触摸webview时,我必须显示弹出窗口,我尝试使用ontouchlistener,但我仍然无法弹出视图。你能帮我解决这个问题吗,先谢谢你。以下是我的代码。

webView.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            touch();
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            touch();
            break;
        case MotionEvent.ACTION_MOVE:
            touch();
            break;
        case MotionEvent.ACTION_UP:
            touch();
            break;
        case MotionEvent.ACTION_POINTER_UP:
            touch();
            break;
        }
        return true;
    }
});

public void touch() {
    View popUpView = getLayoutInflater().inflate(R.layout.popup, null);                                                                         
    mpopup = new PopupWindow(popUpView, LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT, true);
    mpopup.setAnimationStyle(android.R.style.Animation_Dialog);
    mpopup.showAtLocation(popUpView, Gravity.NO_GRAVITY, 0, 0);

    Button option = (Button) popUpView.findViewById(R.id.sign);
    option.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mpopup.dismiss();
            Intent intent = new Intent(View.this,
                Capture.class);
            startActivity(intent);
            finish();
        }
    });
    Button btnOk = (Button) popUpView.findViewById(R.id.date);
    btnOk.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mpopup.dismiss();
        }
    });

应用程序在触摸webview时崩溃了。

popup.xml

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:orientation="vertical"
  android:background="@android:color/transparent"
  android:layout_height="wrap_content">

  <Button
      android:id="@+id/sign"
      android:layout_width="160dp"
      android:layout_height="60dp"
      android:layout_toLeftOf="@+id/camera"
      android:text="button" >
  </Button>

  <Button
      android:id="@+id/date"
      android:layout_width="160dp"
      android:layout_height="60dp"
      android:layout_alignParentRight="true"
      android:layout_alignParentTop="true"
      android:text="button" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

试试这个:

 LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   mpopup = new PopupWindow(inflater.inflate(R.layout.popup, null, false), 100, 100, true);

    // Show the Popup
    mpopup.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0);

您的布局(根容器)必须设置ID,例如:

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/main"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <WebView 
        android:id="@+id/webViewEx1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />


</LinearLayout>

答案 1 :(得分:0)

将WebView设置为

android:clickable="true"

在你的RelativeLayout活动中。

然后,将OnClickListener设置为WebView:

WebView wv = (WebView) findViewById(R.id.webview);
wv.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
         Intent i = new Intent(getApplicationContext(), NextActivity.class);
         startActivity(i);
     }
});