迷你浏览器——WebView学习
MiniBrowser.java
public class MiniBrowser extends Activity { EditText url; WebView showWebView; Button searchButton; String urlStr = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); url = (EditText) findViewById(R.id.url); showWebView = (WebView) findViewById(R.id.show); searchButton = (Button) findViewById(R.id.searchButton); //真机测试时,保证在当前的Activity显示页面,而非自带浏览器显示 showWebView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub //用WebView 载入页面 view.loadUrl(url); return true; } }); //enable JavaScript showWebView.getSettings().setJavaScriptEnabled(true); searchButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub urlStr = url.getText().toString(); showWebView.loadUrl(urlStr); } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub //按后退键可以跳到上级页面 if(keyCode == KeyEvent.KEYCODE_BACK){ showWebView.goBack(); return true; } return super.onKeyDown(keyCode, event); } }mail.xml 布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="match_parent" tools:context=".MiniBrowser" > <EditText android:id="@+id/url" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:singleLine="true" android:layout_toLeftOf="@+id/searchButton" android:layout_alignTop="@+id/searchButton" android:layout_alignBottom="@+id/searchButton" /> <Button android:id="@+id/searchButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="@string/search" /> <WebView android:id="@+id/show" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@id/searchButton"/> </RelativeLayout>
<uses-permission android:name="android.permission.INTERNET"/>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。