无法加载用于警告网页的自定义对话框

时间:2011-09-22 16:50:11

标签: android dialog webview alertdialog

我想提醒用户,如果页面没有加载,并且Toast功能不会长时间保持不变(但我将代码保留并注释掉,以防我想回到它)。所以我现在尝试做一个自定义对话框,但不会弹出。

我基本上打开网页,这有效 我在状态栏中添加了一个加载程序,所以人们会看到页面正在加载,这是有效的 我添加了代码以保持应用程序中的导航,因此人们不会退出到新的浏览器,这是有效的 正如我所说,吐司在技术上有效,但不会像我想的那样长时间保持 然后我添加了自定义警报对话框,这就是我失败的地方

我还为自定义提醒

创建了一个单独的XML文件

然后我还没有达到这个目的,但我是否需要添加代码来关闭它,或者只是按下后退按钮自动关闭它?

谢谢!

这是我在.java文件中的代码

    public class FC extends Activity {

    WebView mWebView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

        //this one line added for progress support
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);

        setContentView(R.layout.web);

        //makes progress bar visible
        getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);

        //get web view
        mWebView = (WebView) findViewById( R.id.webWeb );
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.setInitialScale(45);
        mWebView.loadUrl("http://www.chipmunkmobile.com");

        //sets the Chrome client
        final Activity MyActivity = this;
        mWebView.setWebChromeClient(new WebChromeClient()
        {
            public void onProgressChanged(WebView view, int progress)
            {
                //makes the bar disappear after URL is loaded, and changes string to Loading...
                MyActivity.setTitle("Loading...");
                MyActivity.setProgress(progress * 100);

                //return the app name after finish loading
                if(progress == 100)
                    MyActivity.setTitle(R.string.app_name);
            }

        });

        //makes page stay in same web client
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }
        });


        //looks to see if connects
        mWebView.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                //Toast.makeText(getApplicationContext(), "NO CONNECTION?\nVisit the Cyberspots in the SW and NW Halls to find out how to get on the free WiFi", 
                    //Toast.LENGTH_LONG).show();

                //showDialog(0);

                AlertDialog.Builder builder;
                AlertDialog alertDialog;

                Context mContext = getApplicationContext();
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
                View layout = inflater.inflate(R.layout.custom_dialog,
                        (ViewGroup) findViewById(R.id.layout_root));

                TextView text = (TextView) layout.findViewById(R.id.text);
                text.setText("WHATEVER");
                ImageView image = (ImageView) layout.findViewById(R.id.imgid);
                image.setImageResource(R.drawable.img);

                builder = new AlertDialog.Builder(mContext);
                builder.setView(layout);
                alertDialog = builder.create();


            }
        });
    }

这是我在.xml文件中的代码

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/layout_root"
  android:padding="10dp">
    <ImageView android:id="@+id/imgid"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="10dp" 
        />
    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textColor="#FFF"
        />

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

在方法的末尾添加alertDialog.show()以实际显示对话框。

此外,您可能需要添加setNeutralButton("ok", new DialogInterface.OnClickListener() {...}并调用finish以关闭OnClickListener中的对话框。

答案 1 :(得分:0)

我没有使用警报,而是使用了嵌入式HTML错误消息...我没有找到alertDialog问题的修复程序

相关问题