上节提到用uri访问网
Uri uri = Uri.parse("http://google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
先看布局:在main.xml文件里,仅定义一个ImageButton
<?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="vertical" >
<!-- <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" /> -->
<ImageButton
android:id="@+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
</LinearLayout
在主函数,TestURLActivity.java文件,使用ImageButton按键监听
package envi.url;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
//import android.widget.ImageView;
public class TestURLActivity extends Activity {
/** Called when the activity is first created. */
//private ImageView imageView = null;
private ImageButton imageButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.main);
//imageView = (ImageView)findViewById(R.id.ImageView01);
imageButton = (ImageButton)findViewById(R.id.ImageButton01);
//匿名内部类实现OnClickListener监听
imageButton.setOnClickListener(new ImageButton.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String urlStr ="http://www.microdu.com";//访问android开发网站
Uri uri = Uri.parse(urlStr);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
);
}
}
运行结果是点击button按键进入android开发网站