最简单的方法就是利用android的networking api 打开一个你所选着的网页浏览器,你可以利用它来进入到你网站主页的功能。
1. 建立包含如下参数的android工程:
Project name: BrowserIntent
Build Target: Android 2.2
Application name: BrowserIntent
Package name: com.zy.browserintent
Create Activity: BrowserIntent
Min SDK Version: 8
2. 改变布局文件main.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<EditText
android:id="@+id/url_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:lines="1"
android:inputType="textUri"
android:imeOptions="actionGo"/>
<Button
android:id="@+id/go_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/go_button"/>
</LinearLayout>
在这里实现了两个控制,一个是EditText控制,一个是Button。
在EditText中,我们利用android:layout_weight=”1.0”来使text区域填满在button的左边区域,利用android:lines=”1”来限制子一条垂直线上。由于在android 1.5本版以后支持利用虚拟键盘,则利用android:inputType=”textUri”和android:imeOptions=”actionGo”来显示虚拟键盘,并且把普通的键盘转换成包含”.com”和”/”来方便网站的输入。
3. 配置资源文件strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, BrowserIntent!</string>
<string name="app_name">BrowserIntent</string>
<string name="go_button">Go</string>
</resources>
接下来我们需要来在BrowserIntent文件中来完成onCreate()方法,这是我们来建立界面UI和实现功能的位置。
4. 建立BrowserIntent.class文件如下:
package com.zy.browserintent;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
public class BrowserIntent extends Activity {
private EditText urlText;
private Button goButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get a handle to all user interface elements
urlText = (EditText) findViewById(R.id.url_field);
goButton = (Button) findViewById(R.id.go_button);
// Setup event handlers
goButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
openBrowser();
}
});
urlText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View view, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
openBrowser();
return true;
}
return false;
}
});
}
/** Open a browser on the URL specified in the text box */
private void openBrowser() {
Uri uri = Uri.parse("http:"+urlText.getText().toString());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
在OnCreate()方法中利用setContentView()方法来显示我们的布局文件layout,利用findViewById()方法来控制两个接口EditText和Button。setOnClickListener方法来实现当用户选择button时,我们来运行代码,在本程序里将要运行openBrowser()方法。keyCode == KeyEvent.KEYCODE_ENTER的作用是只有在点击enter键时,才代表一个网址输入结束。虽然在openBrowser方法中只有三行代码,但是这里是最重要的。
记住在输入网址的时候,要加入http://,否则程序将不知道怎样来输出,但是在真正的程序中,可以令用户忽略这个,让程序来自动完成。本人由于在手机中输入http:比较麻烦,则在程序前加"http:"+urlText.getText().toString(),方便查找。