无法一次显示进度条和文本视图

时间:2015-03-16 09:51:57

标签: android android-studio textview progress-bar android-webview

嗨大家我试图一次显示进度条和文本视图,但文本视图只显示一次并消失。下面是我的Java文件:

 public class MainActivity extends Activity {

    public boolean show_splash;
    ProgressBar progressBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        show_splash = true;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView myWebView = (WebView) findViewById(R.id.webview);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);

        //enable Javascript
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setAllowContentAccess(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setSupportMultipleWindows(true);
        webSettings.setSupportZoom(true);
        webSettings.setLoadsImagesAutomatically(true);

        //loads the WebView completely zoomed out
        webSettings.setLoadWithOverviewMode(true);

        webSettings.setUseWideViewPort(true);

        myWebView.setWebViewClient(new MyWebViewClient());    

        myWebView.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent,
                                        String contentDisposition, String mimetype,
                                        long contentLength) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);

            }
        });

        //load the home page URL
        myWebView.loadUrl("http://mvc.myorange.ca/");
    }

    public void Visible(){
        WebView webview = (WebView) findViewById(R.id.webview);
        //ImageView logo = (ImageView) findViewById(R.id.imageView1);
        TextView Text =(TextView) findViewById(R.id.textView1);
        if (show_splash){
            webview.setVisibility(View.INVISIBLE);
            //logo.setVisibility(View.VISIBLE);
            Text.setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.VISIBLE);
            show_splash=false;
        }
        else {
            webview.setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.VISIBLE);
            Text.setVisibility(View.INVISIBLE);
            //logo.setVisibility(View.INVISIBLE);
        }
    }

    public void Invisible(){
        WebView webview = (WebView) findViewById(R.id.webview);
        //ImageView logo = (ImageView) findViewById(R.id.imageView1);
        webview.setVisibility(View.VISIBLE);
        //logo.setVisibility(View.INVISIBLE);
        progressBar.setVisibility(View.GONE);
    }

    public class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals("mvc.myorange.ca")) {
                return false;
            }
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url,  Bitmap facIcon) {
            Visible();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            Invisible();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        WebView myWebView = (WebView) findViewById(R.id.webview);
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

以下是我的活动xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:gravity="center"
tools:context=".MainActivity">

<WebView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    android:paddingBottom="0dp"/>

<ProgressBar
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:layout_gravity="center"
   android:id="@+id/progressBar"
    android:layout_marginBottom="30dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/text"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#FFFFFF"
    android:textSize="65dp"
    android:background="#0582FF"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:gravity="center_vertical|center_horizontal" />

</RelativeLayout>

现在问题是文本视图只出现一次而且消失了。我希望它首次显示在textview之后的进度条。任何人都可以指出我错在哪里吗?

3 个答案:

答案 0 :(得分:0)

如果要使用对话框

加载文本,请使用ProgressDialog而不是ProgressBar

答案 1 :(得分:0)

做一件事......试着删除这一行 Text.setVisibility(View.INVISIBLE); 并告诉我textview是否停留在那里或仍然消失

答案 2 :(得分:0)

有关获得更好代码的一些评论。

- 变量名称应为小写。 TextView文本。

-findViewById()是一个昂贵的调用,您应该在onCreate方法中执行此操作并将所有视图保存为变量以保存调用,就像使用ProgressBar但使用所有视图一样。

只有几个好的做法。

我发现错误,问题是你把TextView从上到下延伸,从开始到结束,然后它只显示自己。删除这些陈述!

将布局更改为此布局并查看差异:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    android:paddingBottom="0dp"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Texto cualquiera"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#FFFFFF"
        android:textSize="65dp"
        android:background="#0582FF"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:gravity="center_vertical|center_horizontal" />


    <ProgressBar
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_gravity="center"
        android:id="@+id/progressBar"
        android:layout_marginBottom="30dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <WebView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_below="@+id/progressBar"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="0dp"
        android:paddingRight="0dp"
        android:paddingTop="0dp"
        android:paddingBottom="0dp"/>

</RelativeLayout>