●(一)新增一個按鈕,點擊可以直接連到goolge網址
**(Action_view,uri)
1)瀏覽器=>Http://
2)電話=>tel:
搭配uri物件
1.介面設定button(大小字型等),指定id(btnGoogle)和函式名稱(btnGoogle)
2.定義變數,抓id,寫功能
public class Ex2 extends AppCompatActivity {
Button home1,btnGoogle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ex2);
home1=(Button)findViewById(R.id.home1);
btnGoogle=(Button)findViewById(R.id.btnGoogle);
}
public void home(View v){
Intent in=new Intent();
in.setClass(Ex2.this,MainActivity.class);
startActivity(in);
}
public void btnGoogle(View v){
Uri google=Uri.parse("http://www.google.com.tw");
Intent in=new Intent(Intent.ACTION_VIEW,google);
startActivity(in);
}
}
**說明:
1.要先產生uri物件, Uri google=Uri.parse("http://www.google.com.tw");=>透過google(可隨意定)轉成Uri物件,因為Uri是static所以不用new,parse()=>是把字串轉成uri物件類型的方法
2.我們要用的是 Intent(Intent.ACTION_VIEW,google);=>這是Intent的建構式, ACTION_VIEW,前面的 ACTION是動作, _VIEW,是要做的功能
(二)增加一個撥電話的按鈕
public class Ex2 extends AppCompatActivity {
Button home1,btnGoogle,btnTel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ex2);
home1=(Button)findViewById(R.id.home1);
btnGoogle=(Button)findViewById(R.id.btnGoogle);
btnTel=(Button)findViewById(R.id.btnTel);
}
public void btnTel(View v){
Uri tel=Uri.parse("tel:21345678");
Intent in = new Intent(Intent.ACTION_DIAL,tel);
startActivity(in);
}
.....(下面程式與上面同先省略)
}
**說明:
1.先定義變數,抓id
2.這邊撥電話在parse中要輸入("tel:....")=>大小寫都可以
3.action要選擇ACTION_DAIL
(三)增加一個啟動相機的按鈕
public class Ex2 extends AppCompatActivity {
Button home1,btnGoogle,btnTel,btnCam;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ex2);
home1=(Button)findViewById(R.id.home1);
btnGoogle=(Button)findViewById(R.id.btnGoogle);
btnTel=(Button)findViewById(R.id.btnTel);
btnCam=(Button)findViewById(R.id.btnCam);
}
public void btnCam (View v){
Intent in=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(in);
}
...
}
**說明:
1.開啟相機要用 MediaStore.ACTION_IMAGE_CAPTURE
2.這次用到的建構式為Intent(String action),只放一個action當引數
(四)增加一個啟動設定好位置的google map
public class Ex2 extends AppCompatActivity {
Button home1,btnGoogle,btnTel,btnCam,btnMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ex2);
home1=(Button)findViewById(R.id.home1);
btnGoogle=(Button)findViewById(R.id.btnGoogle);
btnTel=(Button)findViewById(R.id.btnTel);
btnCam=(Button)findViewById(R.id.btnCam);
btnMap=(Button)findViewById(R.id.btnMap);
}
public void btnMap(View v){
Uri map=Uri.parse("geo:25.096275, 121.516642");
Intent in=new Intent(Intent.ACTION_VIEW,map);
startActivity(in);
}
**說明:
一樣使用Uri.parse(),但裡面放geo:緯度,經度,同樣使用ACTION_VIEW
*衍生練習(github):ACTION_VIEW的實作練習
文章標籤
全站熱搜
留言列表