从Activity中访问Fragment中的TextView

时间:2016-07-20 12:41:28

标签: android android-fragments android-activity

我正在尝试从主要活动访问片段类中的TextView。我无法从主要活动中获取片段实例。我希望能够在加载WebView时在TextView中显示url。当我运行app我可以在logcat中看到url标题被检索,但我无法找到一种方法在片段TextView中显示它

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.widget.TextView;

public class MainActivity extends FragmentActivity {

    private WebViewFragment mWebViewFragment;
    public static TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        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();
        }
    }
}

WebFragment活动

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);
                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) {
        Activity activity = getActivity();
    }

}

1 个答案:

答案 0 :(得分:2)

我假设您要在setTitle()方法中访问活动。

在你的片段中:

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);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                setTitle(view, title);
                Log.v(getClass().getName(), "Received Title" + title);
            }
        });

    }
}

public void setTitle(String title) {
    Activity activity = getActivity();
    if(activity instanceof MainActivity) {
        ((MainActivity) activity).setTitle(title);
    }
}

在你的MainActivity

public class MainActivity extends FragmentActivity {

    // Remove STATIC from the view to avoid memory leaks
    public TextView textView;

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