Android WebView

布局文件
<?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;
			}
		}
    	
    }
 
}
上图

 




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值