效果如图:
**老规矩,最后有源码。
步骤:
- 在大管家文件中添加网络权限**
2. 超简单的 webview 的实现:三行
代码如下:
布局文件:**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp">
<Button
android:id="@+id/passon"
android:layout_width="61dp"
android:layout_height="36dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:text="转入" />
<Button
android:id="@+id/themain"
android:layout_width="212dp"
android:layout_height="37dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="0dp"
android:text="首页" />
<EditText
android:id="@+id/myurl"
android:layout_width="320dp"
android:layout_height="45dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:ems="10"
android:hint="请输入要转入的网址"
android:maxLines="1"
/>
</RelativeLayout>
<WebView
android:id="@+id/myweb"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</LinearLayout>
复制代码
java 文件:
public class MainActivity extends Activity implements View.OnClickListener {
private WebView myweb;
private String baidu = "http://www.baidu.com";
private Button passon;
private Button themain;
private EditText myurl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
myweb.getSettings().setJavaScriptEnabled(true);//调用getSettings()方法添加属性使webview支持JavaScript的脚本
myweb.setWebViewClient(new WebViewClient());//使其跳转后依然使用webview来显示
myweb.loadUrl(baidu);//利用loadUrl()显示网址
}
private void initView() {
myweb = (WebView) findViewById(R.id.myweb);
passon = (Button) findViewById(R.id.passon);
passon.setOnClickListener(this);
themain = (Button) findViewById(R.id.themain);
themain.setOnClickListener(this);
myurl = (EditText) findViewById(R.id.myurl);
myurl.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.passon://转入按钮
String passonUri =myurl.getText().toString();
if (!passonUri.equals("")){
myweb.loadUrl(passonUri);
}else{
Toast.makeText(this,"请输入网址",Toast.LENGTH_SHORT).show();
}
break;
case R.id.themain://首页按钮
myweb.loadUrl(baidu);
break;
}
}
}
复制代码
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END