ANDROID : 单击按钮时创建 WebView

ANDROID : create a WebView when clicking a Button

I'm trying to add this WebView to onClick function of a button but, WebView is always there before even click the button. I want WebView appear only when clicked . can anyone tell which code I need to put to get the WebView work when the button clicked .

<WebView
    android:id="@+id/webView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center" />

xml代码

private SectionsPagerAdapter mSectionsPagerAdapter;

private ViewPager mViewPager;
protected Button mButton;
private WebView webview;
ViewPager mHolder;

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

    Button mButton = (Button) findViewById(R.id.infobutton);
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            webview = (WebView) findViewById(R.id.webView);
            webview.getSettings().setJavaScriptEnabled(true);

            String chtml = "<html>"+
                    "<style> div{height:50%; width:70%; background-color:#aaa;} body div:nth-child(2){height:20%; margin:20px; width:30%; background-color:#222;} h1{ color:red; font-size:24px;}</style>" +
                    "<body><h1>hello, webview</h1>  <div></div> <div></div> </body></html>";
            webview.loadData(chtml, "text/html",  "UTF-8");

        }
    });

java代码

为此,您可以在 xml 中将可见性设置为 GONE,然后在单击按钮时需要将 webview 的可见性设置为 VISIBLE。

<WebView
    android:id="@+id/web"
    android:visibility="invisible"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center" />

做这个美国布局不要忘记添加 android:能见度="invisible"

在java

public class MainActivity extends AppCompatActivity {

 WebView web;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fgfg);
    Button bt = (Button)findViewById(R.id.button);
     web =(WebView)findViewById(R.id.web);
    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            web.setVisibility(View.VISIBLE);
            startWebView("http://www.youtube.com");
        }
    });
}

点击内部按钮

终于找到了将 WebView 添加到 api 数据的方法

create button

protected WebView eventDesctibtion;

assign button to view

eventDesctibtion = (WebView) findViewById(R.id.corporateDescription);
        WebSettings settings= eventDesctibtion.getSettings();
        settings.setDefaultFontSize(11);

load data

eventDesctibtion.loadData(object.getString("description"), "text/html; charset=utf-8", "UTF-8");