1

我的 MainActivity 中有一个 EditText 视图,用户可以输入一个 url。此 MainActivity 还包含一个将保存 WebView 的片段。

我进行了设置,以便在显示片段时 URL 将加载到 WebView 中。但是我不知道如何将字符串传递给片段?

以下是主要活动代码:

public class MainActivity extends FragmentActivity {

Button goBtn;
EditText urlInput;
String url;

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

    goBtn = (Button)findViewById(R.id.button1);
    urlInput = (EditText)findViewById(R.id.editText1);

    goBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            url = "http://"+urlInput.getText().toString(); //THIS TO FRAGMENT!

            Toast.makeText(v.getContext(), "Search:" + url, Toast.LENGTH_SHORT).show();
        }
    });

这是片段代码:

    WebView webDisplay;
String url;
AsyncHttpClient client;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.frag1_layout, container, false);
    urlDisplay = (TextView)v.findViewById(R.id.textView1);
    webDisplay = (WebView)v.findViewById(R.id.webView1);
    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    //url = "http://"+this.getActivity() ???
    client = new AsyncHttpClient();
    client.get(url, new AsyncHttpResponseHandler(){

        @Override
        public void onSuccess(String response) {
            Toast.makeText(getActivity(), "Success!", Toast.LENGTH_SHORT).show();
            webDisplay.setWebViewClient(new WebViewClient() {
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                          view.loadUrl(url);
                          return true;
                }}); 
            webDisplay.loadUrl(url);
        }
    });

我关心的值是 MainActivity.java 中的 String URL 变量。

片段的事务由 TabFragment 类控制。

public class TabFragment extends Fragment {

private static final int TAB1_STATE = 0x1;
private static final int TAB2_STATE = 0x2;
private int mTabState;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View v  = inflater.inflate(R.layout.fragment_tab, container, false);
    //References to buttons in layout file
    Button tabBtn1 = (Button)v.findViewById(R.id.tabBtn1);
    Button tabBtn2 = (Button)v.findViewById(R.id.tabBtn2);

    //add listener to buttons
    tabBtn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            //Action to perform when Tab 1 clicked...
            goToTab1View();

        }
    });

    tabBtn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Action to perform when Tab 2 clicked...
            goToTab2View();
        }
    });

    return v;

}

//Tab action functions.
protected void goToTab1View() {
    if(mTabState != TAB1_STATE){
        mTabState = TAB1_STATE;
        FragmentManager fm = getFragmentManager();
        if(fm!= null){
            //Perform fragment transaction
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.fragment_content, new FragTab1());
            ft.commit();
        }
    }
}

protected void goToTab2View() {
    if(mTabState != TAB2_STATE){
        mTabState = TAB2_STATE;
        FragmentManager fm = getFragmentManager();
        if(fm!= null){
            //Perform fragment transaction
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.fragment_content, new FragTab2());
            ft.commit();
        }
    }

}
4

2 回答 2

6

或者Bundle使用方法创建和设置它fragment.setArguments(myBundle);

于 2013-01-11T01:45:31.673 回答
2

这个答案是在片段已经存在的情况下

您可以在片段中定义公共方法。就像是:

public void setString(final String str) { //... }

之后,让您的活动实现 OnClickListener 接口。当按下按钮时,只需键入以下内容:

public void onClick(View view) {
// Form url...
((YourWebViewFragment) getFragmentManager().findFragmentById(fragments_id)).setString(url);
}
于 2013-01-11T01:43:42.920 回答