【发布时间】:2017-02-08 11:05:45
【问题描述】:
我正在尝试将此 WebView 添加到按钮的 onClick 功能,但是, WebView 在单击按钮之前始终存在。我想要网页视图 仅在单击时出现。谁能告诉我需要输入哪个代码 单击按钮时获取 WebView 工作。
<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 文件中使 webview 的可见性
GONE并使其在按钮单击时可见?
标签: android android-layout android-webview android-xml android-button