布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="1"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/editText1"> <requestFocus></requestFocus> </EditText> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:text="确定" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="取消" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> <WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="match_parent"></WebView> </LinearLayout>
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Mp3infoActivity extends Activity { /** Called when the activity is first created. */ private Button button_Ok,button_Reset; private WebView webView; private EditText editText; private WebSettings webSettings; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button_Ok=(Button)findViewById(R.id.button1); button_Reset=(Button)findViewById(R.id.button2); webView=(WebView)findViewById(R.id.webView1); webSettings=webView.getSettings(); webSettings.setJavaScriptEnabled(true);//支持JS webSettings.setUseWideViewPort(true);让浏览器支持用户自定义view webSettings.setBuiltInZoomControls(true);//支持缩放 webSettings.setAllowFileAccess(true);// 允许访问android系统数据文件 editText=(EditText)findViewById(R.id.editText1); button_Ok.setOnClickListener(new ButtonListenr()); button_Reset.setOnClickListener(new ButtonListenr()); } private class ButtonListenr implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1: String stringUrl=editText.getText().toString(); stringUrl=stringUrl.trim();//去掉空格 if (stringUrl.length()==0) { Toast.makeText(getApplicationContext(), "网址为空", Toast.LENGTH_LONG).show(); }else { stringUrl="http://ting.baidu.com/search?key="+stringUrl; webView.loadUrl(stringUrl); } break; case R.id.button2: editText.setText(null); break; default: break; } } } }
上图
Android WebView
最新推荐文章于 2024-06-07 22:39:00 发布