当前位置: 移动技术网 > IT编程>移动开发>Android > android打电话(callphone)案例

android打电话(callphone)案例

2018年04月25日  | 移动技术网IT编程  | 我要评论

适宝康官网,尺子标准图,移风易俗倡议书

MainActivity.java

 1 package com.example.callthone;
 2 
 3 import java.net.URI;
 4 
 5 import com.example.click4.R;
 6 
 7 import android.net.Uri;
 8 import android.os.Bundle;
 9 import android.app.Activity;
10 import android.content.Intent;
11 import android.view.Menu;
12 import android.view.View;
13 import android.view.View.OnClickListener;
14 import android.widget.Button;
15 import android.widget.EditText;
16 import android.widget.Toast;
17 
18 public class MainActivity extends Activity implements OnClickListener {
19 
20     private EditText ed1;
21 
22     @Override
23     protected void onCreate(Bundle savedInstanceState) {
24         super.onCreate(savedInstanceState);
25         setContentView(R.layout.activity_main);
26         ed1 = (EditText) findViewById(R.id.editText1);
27         Button btn_call = (Button) findViewById(R.id.button1);
28 
29         Button btn_call2 = (Button) findViewById(R.id.button2);
30         Button btn_call3 = (Button) findViewById(R.id.button3);
31         Button btn_call4 = (Button) findViewById(R.id.button4);
32         // 按钮的第二种事件
33         // 匿名内部类
34 
35         // 第三种事件
36         btn_call.setOnClickListener(this);
37         btn_call2.setOnClickListener(this);
38         btn_call3.setOnClickListener(this);
39         btn_call4.setOnClickListener(this);
40 
41         // btn_call.setOnClickListener(new OnClickListener() {
42         //
43         // @Override
44         // public void onClick(View arg0) {
45         // call_phone();
46         //
47         // }
48         // });
49     }
50 
51     private void call_phone() {
52         String ed_number = ed1.getText().toString().trim();
53         if ("".equals(ed_number)) {
54             Toast.makeText(MainActivity.this, "num不能为空", Toast.LENGTH_LONG)
55                     .show();
56             return;
57         }
58         Intent intent = new Intent();
59         intent.setAction(Intent.ACTION_CALL);
60 
61         intent.setData(Uri.parse("tel:" + ed_number));
62         startActivity(intent);
63 
64         System.out.println("按钮被点击了" + ed_number);
65     }
66 
67     @Override
68     public void onClick(View v) {
69         // TODO Auto-generated method stub
70         switch (v.getId()) {
71         case R.id.button1:
72             call_phone();
73             break;
74         case R.id.button2:
75             call_phone();
76             break;
77         case R.id.button3:
78             call_phone();
79             break;
80         case R.id.button4:
81             System.out.println("我被削了");
82             break;
83 
84         default:
85             break;
86         }
87     }
88 
89 }

activity_main.xml

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context=".MainActivity" >
 6 
 7     <TextView
 8         android:id="@+id/textView1"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:text="请输入手机号:"
12         android:textSize="19sp" />
13 
14     <EditText
15         android:id="@+id/editText1"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:layout_alignParentLeft="true"
19         android:layout_below="@+id/textView1"
20         android:ems="10" >
21 
22         <requestFocus />
23     </EditText>
24 
25     <Button
26         android:id="@+id/button1"
27         android:layout_width="wrap_content"
28         android:layout_height="wrap_content"
29         android:layout_alignParentLeft="true"
30         android:layout_below="@+id/editText1"
31         android:text="拨打" />
32 
33     <Button
34         android:id="@+id/button2"
35         android:layout_width="wrap_content"
36         android:layout_height="wrap_content"
37         android:layout_alignBaseline="@+id/button1"
38         android:layout_alignBottom="@+id/button1"
39         android:layout_marginLeft="27dp"
40         android:layout_toRightOf="@+id/button1"
41         android:text="拨打2" />
42 
43     <Button
44         android:id="@+id/button3"
45         android:layout_width="wrap_content"
46         android:layout_height="wrap_content"
47         android:layout_below="@+id/editText1"
48         android:layout_marginLeft="23dp"
49         android:layout_toRightOf="@+id/button2"
50         android:text="拨打3" />
51 
52     <Button
53         android:id="@+id/button4"
54         android:layout_width="wrap_content"
55         android:layout_height="wrap_content"
56         android:layout_alignLeft="@+id/button2"
57         android:layout_below="@+id/button2"
58         android:layout_marginLeft="15dp"
59         android:layout_marginTop="24dp"
60         android:text="拨打4" />
61 
62 </RelativeLayout>


在AndroidManifest添加android.permission.CALL_PHONE权限

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网