单击按钮打开一个网址hao123实例
package com.example.wudi;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent; //引用intent类
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener( //给按钮添加一个事件打开网址hao123
new OnClickListener(){
public void onClick(View v)
{
Intent intent=new Intent(); //建立一个intent
Uri uri=Uri.parse("http://www.hao123.com"); //设置uri
intent.setAction(Intent.ACTION_VIEW); //设置建立的intent动作是显示
intent.setData(uri); //设置intent的数据是打开uri中的内容
startActivity(intent); //通过startActivity打开新的intent---直接打开网址hao123
}
}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}