加载网页视图时隐藏后退按钮

时间:2016-07-21 10:36:11

标签: android android-fragments android-webview visibility

我正在尝试在我的应用加载时使按钮不可见,并在webview加载url页面时使其可见。首先,我想从主要活动中访问我的片段类中的按钮。我试过但不能。但是我没有运气。请帮忙。

MainActivity

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

    private WebViewFragment mWebViewFragment;
    public TextView textView;
    public Button buttons;

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

        buttons = (Button) findViewById(R.id.button);
        buttons.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        textView = (TextView) findViewById(R.id.textViewId);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        mWebViewFragment = new WebViewFragment();
        fragmentTransaction.replace(R.id.mainFragment, mWebViewFragment);
        fragmentTransaction.commit();
    }

    @Override
    public void onBackPressed() {

        if(mWebViewFragment != null && mWebViewFragment.canGoBack()) {
            mWebViewFragment.goBack();
        } else {
            super.onBackPressed();
        }
    }

    public void setTitle(String title) {
        if(textView != null){
            textView.setText(title);
        }
    }

    public void buttonVisible() {
        if(buttons != null){
            buttons.setVisibility(View.VISIBLE);
        }
    }

    public void buttonInvisible() {
        if(buttons != null) {
            buttons.setVisibility(View.INVISIBLE);
        }
    }

}

片段类

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewFragment extends Fragment {

    private WebView mWebView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        mWebView = (WebView) view.findViewById(R.id.webView);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("https://www.google.co.uk/");
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                String title = mWebView.getTitle();
                Log.v(getClass().getName(), "Title=" + title);
            }
        });

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                    setTitle(title);

                Log.v(getClass().getName(), "Received Title" + title);
            }
        });
        return view;
    }

    public boolean canGoBack() {
        return mWebView.canGoBack();
    }

    public void goBack() {
        mWebView.goBack();
    }

    public void setTitle(String title) {

        if (title != null && title.length() > 0) {
            Activity activity = getActivity();
            if(activity != null && activity instanceof MainActivity){
                ((MainActivity) activity).setTitle(title);
            }
        }
    }


   public void buttonVisible() {
           Activity activity = getActivity();
           if(activity != null && activity instanceof MainActivity){
               ((MainActivity) activity).setVisible(true);
           }

   }

    public void buttonInVisible() {
        Activity activity = getActivity();
        if(activity != null && activity instanceof MainActivity){
            ((MainActivity) activity).setVisible(false);
        }

    }

}

0 个答案:

没有答案