標籤:tco margin edittext btn override script content make def
這一次我對網頁的瀏覽添加前進後退,退出,返回首頁的功能。
上代碼,
public class MainActivity extends Activity implements View.OnClickListener { private EditText et_url; private Button btn_visit; private WebView web; private Button btn_home,btn_left,btn_right,btn_exit; private boolean isExit; private String homeStr = "http://www.baidu.com"; //------------------------------------------- public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); init(); } //------------------------------------------- /** * 基本資料初始化 */ private void init(){ isExit = false; et_url = (EditText)findViewById(R.id.et_url); et_url.setText(homeStr); btn_visit = (Button)findViewById(R.id.btn_visit); web = (WebView)findViewById(R.id.web); WebSettings ws = web.getSettings(); //是否允許指令碼支援 ws.setJavaScriptEnabled(true); web.setWebViewClient(wvc); btn_home = (Button)findViewById(R.id.btn_home); btn_left = (Button)findViewById(R.id.btn_left); btn_right = (Button)findViewById(R.id.btn_right); btn_exit = (Button)findViewById(R.id.btn_exit); btn_visit.setOnClickListener(this); btn_home.setOnClickListener(this); btn_left.setOnClickListener(this); btn_right.setOnClickListener(this); btn_exit.setOnClickListener(this); conn(et_url.getText().toString()); } //------------------------------------------- WebViewClient wvc = new WebViewClient(){ public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); et_url.setText(url); return true; }; }; //------------------------------------------- /** * 訪問url * @param urlStr */ private void conn(String urlStr){ String url = ""; if(urlStr.contains("http://")){ url = urlStr; }else{ url = "http://"+urlStr; } web.loadUrl(url); } //------------------------------------------- /** * 後退 */ private void goBack(){ if(web.canGoBack()){ web.goBack(); }else{ Toast.makeText(this, "已經是第一頁", Toast.LENGTH_SHORT).show(); } } //------------------------------------------- /** * 前進 */ private void goForward(){ if(web.canGoForward()){ web.goForward(); }else{ Toast.makeText(this, "已經是最後一頁",Toast.LENGTH_SHORT).show(); } } //------------------------------------------- /** * 退出 */ private void exit(){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("提示") .setMessage("確定退出?") .setPositiveButton("確定", dialogListener) .setNegativeButton("取消", dialogListener) .create() .show(); } DialogInterface.OnClickListener dialogListener = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { switch (which) { case DialogInterface.BUTTON_POSITIVE: finish(); break; default: dialog.cancel(); break; } } }; //------------------------------------------- public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_BACK||web.canGoBack()){ web.goBack(); if(!web.canGoBack()){ if(isExit){ return super.onKeyDown(keyCode, event); } isExit = true; Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show(); }else{ isExit = false; } } return true; } //------------------------------------------- public void onClick(View v) { switch (v.getId()) { case R.id.btn_visit: conn(et_url.getText().toString()); break; case R.id.btn_home: conn(homeStr); break; case R.id.btn_left: goBack(); break; case R.id.btn_right: goForward(); break; case R.id.btn_exit: exit(); break; } }}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ScrollView android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/layout" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dp"> <EditText android:id="@+id/et_url" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:inputType="textUri" android:singleLine="true" android:layout_margin="10dp"/> <Button android:id="@+id/btn_visit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="visit" android:textSize="15dp" /> </LinearLayout> <WebView android:id="@+id/web" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout> </ScrollView> <LinearLayout android:id="@+id/ll_btn" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <Button android:id="@+id/btn_home" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="home"/> <Button android:id="@+id/btn_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="left"/> <Button android:id="@+id/btn_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="right"/> <Button android:id="@+id/btn_exit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="exit"/> </LinearLayout></LinearLayout>
<uses-permission android:name="android.permission.INTERNET"/>
因為這其實是從上一篇的基礎上加了一些UI設計罷了,沒啥知識點,下期我做網頁手指觸摸放大放小,請期待吧
再見
Android 自製瀏覽器 二 前進後退返回首頁退出