选项卡页面中的Web视图android应用程序

时间:2016-07-26 12:03:32

标签: android

我想问这个简单的问题 我在应用程序中的一个选项卡上有这个java类: -

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Tab3 extends android.support.v4.app.Fragment {
    private static final String ARG_SECTION_NUMBER = "section_number";

    public static Tab3 newInstance(int sectionNumber) {
        Tab3 fragment = new Tab3();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v =inflater.inflate(R.layout.tab3,container,false);
        return v;
    }
}

我希望在上面的代码中添加此Web视图代码: -

public class MainActivity extends AppCompatActivity {
    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("http://google.com");
    }
}

我怎么能在两个代码之间混合,因为我尝试了很多,我失败了因为我仍然是在android中的begginers 请帮我 谢谢

如果我想添加此代码: -

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MyAndroidAppActivity extends Activity {

    private ToggleButton toggleButton1, toggleButton2;
    private Button btnDisplay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        addListenerOnButton();

    }

    public void addListenerOnButton() {

        toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);
        toggleButton2 = (ToggleButton) findViewById(R.id.toggleButton2);
        btnDisplay = (Button) findViewById(R.id.btnDisplay);

        btnDisplay.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                StringBuffer result = new StringBuffer();
                result.append("toggleButton1 : ").append(
                        toggleButton1.getText());
                result.append("\ntoggleButton2 : ").append(
                        toggleButton2.getText());

                Toast.makeText(MyAndroidAppActivity.this, result.toString(),
                        Toast.LENGTH_SHORT).show();

            }

        });

    }
}

如何在标签代码和最后一个代码之间混合

2 个答案:

答案 0 :(得分:1)

试试这个

public class Tab3 extends android.support.v4.app.Fragment {
    private static final String ARG_SECTION_NUMBER = "section_number";

    public static Tab3 newInstance(int sectionNumber) {
        Tab3 fragment = new Tab3();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v =inflater.inflate(R.layout.tab3,container,false);
       WebView mWebView = (WebView) v.findViewById(R.id.activity_main_webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("http://google.com");
        return v;
    } }
  

在R.layout.tab3的XML中添加webview

答案 1 :(得分:1)

试一试

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v =inflater.inflate(R.layout.tab3,container,false);
 mWebView = (WebView) v.findViewById(R.id.activity_main_webview);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.loadUrl("http://google.com");
    return v;
}