0

我的应用程序顶部有一个 WebView。为了展示东西,我使用 Toast。但是,如果出现 WebView,则 Toast 框从未显示,而在 WebView 显示之前,Toast 是可见的。我想知道 WebView 是否可能涵盖 Toast。有没有人有同样的问题?

谢谢。

编辑!!!嗨,感谢您的所有投入。我发现我针对我的问题发布了一个错误的问题。我发现真正的问题如下。在代码中,我使用 setOnClickListener 为按钮注册回调。在调试中发现由于某种原因没有调用回调,所以没有调用Toast语句,也没有被webview覆盖。

然后,我尝试了 xml 布局中的 onclick 属性来定义按钮的回调 clickGo。当我按下按钮时,这个可以工作,吐司会显示出来。

现在,我的问题是 setOnClickListener 和 onlick 有什么区别。

还有一个问题,在我的 clickGo 中,我刷新了 webview。单击按钮时,webview 确实会重新加载。但与此同时,微调器也被重新加载,选择位置被重置为第 0 个。我怎样才能防止这种情况?

再次感谢你!

    public class MainActivity extends Activity {

      JSONArray jArray;
      String result = null;
      InputStream is = null;
      StringBuilder sb=null;
      private Spinner spinner;
      private Button btnSubmit;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addListenerOnButton();
        addListenerOnSpinnerItemSelection();
        new DownloadTask().execute("www.google.com");
      }

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
      }

      // Given a string representation of a URL, sets up a connection and gets
      // an input stream.
      private String downloadUrl(String urlString) throws IOException {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet get = new HttpGet(urlString);

        HttpResponse response = httpClient.execute(get);

        // Build up result
        return EntityUtils.toString(response.getEntity());
      }

      public void addListenerOnSpinnerItemSelection() {
        spinner = (Spinner) findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
      }

      public void clickGo(View v) {
        Toast toast = Toast.makeText(MainActivity.this,
            "OnClickListener : " +
              "\nSpinner 1 : "+ String.valueOf(spinner.getSelectedItem()),
                    Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.TOP, 0, 0);
        toast.show();
        new DownloadTask().execute("www.google.com");
      }

      // get the selected dropdown list value
      public void addListenerOnButton() {

        spinner = (Spinner) findViewById(R.id.spinner);
        btnSubmit = (Button) findViewById(R.id.button);

        btnSubmit.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {

            Toast toast = Toast.makeText(MainActivity.this,
            "OnClickListener : " +
                "\nSpinner 1 : "+ String.valueOf(spinner.getSelectedItem()),
                    Toast.LENGTH_SHORT);
          toast.setGravity(Gravity.TOP, 0, 0);
          toast.show();
          new DownloadTask().execute("www.google.com");

        }

      });
    }
`
    // Implementation of AsyncTask used to download XML feed from stackoverflow.com.
    private class DownloadTask extends AsyncTask<String, Void, String> {

      @Override
      protected String doInBackground(String... urls) {
         return downloadUrl(urls[0]);
      }

      @Override
      protected void onPostExecute(String result) {
        setContentView(R.layout.activity_main);
        // Displays the HTML string in the UI via a WebView
        WebView myWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl(result);
      }
    }
    }



    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent" 
                  android:layout_height="fill_parent" 
                  android:orientation="vertical" >
   <LinearLayout android:layout_width="match_parent" 
                     android:layout_height="wrap_content" 
                     android:orientation="horizontal" >
          <TextView android:id="@+id/text"
                    android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                    android:text="@string/word" />
          <Button android:id="@+id/button"
              android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Go" 
                  android:onClick="clickGo" />
    </LinearLayout>
  <Spinner android:id="@+id/spinner"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:entries="@array/dictionaries"
           android:prompt="@string/dict_prompt" />      

  <WebView android:id="@+id/webview"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent" />
    </LinearLayout>
4

3 回答 3

0

没有吐司以最大绘制顺序绘制,因为它们是在运行时创建的。确保您在哪里创建吐司以及它是否真的被调用。
编辑
如果您的问题与绘制顺序有关,请尝试使用此代码

private void moveViewToFront(View currentView)
    {
        ViewGroup vg = ((ViewGroup) currentView.getParent());
        vg.bringChildToFront(vg.getChildAt(vg.indexOfChild(currentView)));
    }

moveViewToFront((LinearLayout) findViewById(R.id.main_layout_2_linear));//pass your ui element you want to bring to the top
于 2013-07-02T13:29:05.870 回答
0

为此,我们需要在 android 中为我们在 xml 中添加的 WebView 添加一个 webviewclient。然后我们必须将 webviewclient 注册到使用此方法创建的 WebView 中。

 myWebView.setWebViewClient(new MyWebViewClient());

现在,创建一个新项目并将其命名为 WebViewDemo。在 main.xml 中,

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

现在在 assets 文件夹中创建一个名为 test.html 的文件并将此代码复制到其中。这是我们加载到 webview 中的 html 文件。

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN"
    "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
<html>
<body>
<a href="http://www.google.com">Google</a>
<input type="button" value="Click Me" onClick="showAndroidToast('Hello Google!')"     
/>
<script type="text/javascript">
    function showAndroidToast(toast) {
        Android.showToast(toast);
    }

</script>
</body>
</html>

现在在java代码中引用webview并加载html文件

WebView myWebView;
myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("file:///android_asset/test.html");

现在通过调用此函数启用 javascript

WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

现在我们必须添加用于监听我们在 webview html 文件中定义的 javascript 函数的 javascript 接口。

myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

// outside oncreate
public class JavaScriptInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    JavaScriptInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        startActivity(new Intent(WebViewDemo.this, WebViewDemo.class));
    }
}

现在创建一个 webview 客户端,用于监听浏览器活动并执行特定功能。

myWebView.setWebViewClient(new MyWebViewClient());

private class MyWebViewClient extends WebViewClient {
   @Override
   public boolean shouldOverrideUrlLoading(WebView view, String url) {
       if (Uri.parse(url).getHost().equals("www.coderzheaven.com")) {
           Toast.makeText(getApplicationContext(), "www.coderzheaven.com",      
Toast.LENGTH_SHORT).show();
           return false;
       }
       // Otherwise, the link is not for a page on my site, so launch another Activity                      
that handles URLs
       Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
       startActivity(intent);
       return true;
   }
}

现在我们听后退按钮。

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
   // Check if the key event was the BACK key and if there's history
   if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
       myWebView.goBack();
       return true;
   }
   return super.onKeyDown(keyCode, event);
  }

现在项目结束了。现在运行并查看结果。这是此示例的完整 java 代码

package com.coderzheaven.pack;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class WebViewDemo extends Activity {

WebView myWebView;
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myWebView = (WebView) findViewById(R.id.webview);
    myWebView.loadUrl("file:///android_asset/test.html");
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

   // myWebView.setWebViewClient(new WebViewClient());
    myWebView.setWebViewClient(new MyWebViewClient());
  }

  public class JavaScriptInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    JavaScriptInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        startActivity(new Intent(WebViewDemo.this, WebViewDemo.class));
    }
  }

  private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.google.com")) {
            // This is my web site, so do not override; let my WebView load the page
            Toast.makeText(getApplicationContext(), "www.google.com",       
  Toast.LENGTH_SHORT).show();
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity   
  that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
  }

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the BACK key and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
        myWebView.goBack();
        return true;
    }
    // If it wasn't the BACK key or there's no web page history, bubble up to the 
  default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
  }
} 
于 2013-07-02T13:31:03.963 回答
0

我认为这个问题可以解决你的问题。

如果可以显示,那么我认为吐司不应该被 WebView 覆盖。

onPageFinished() 从未调用过(webview)!

mWebView.setWebViewClient(new WebViewClient() {
@Override  
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(mWebView, url);
    Toast.makeText(getApplicationContext(), "Done!", Toast.LENGTH_SHORT).show();
}  

@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
    Toast.makeText(getApplicationContext(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
}

}); mWebView.loadUrl(" http://pabebbe.com/m/register ");

于 2013-07-02T13:23:10.750 回答