01_电话拨号器工程

来源:互联网 发布:linux 配置代理服务器 编辑:程序博客网 时间:2024/05/21 11:07

*第一个安卓工程电话拨号器

第一:在布局文件中添加相应的组建

<TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="请输入号码" />    <EditText         android:id="@+id/et_phone"        android:layout_width="match_parent"        android:layout_height="wrap_content"        />    <Button         android:id="@+id/bt_call"         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="拨打" />        />
#说明:布局文件中所有的标签都对应着一个android的类,所有组件类的超级父类为View 类,View类中的

indViewById(R.id.bt_call)//需要强制类型转换

方法可以通过id获取所有的类,自己添加的组件都需要自定义一个ID.

第二:在ManiActivity类中给按钮设置监听

public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // 给按钮设置点击侦听        // 1.拿到按钮对象        // Button bt = new Button(context)        Button bt = (Button) findViewById(R.id.bt_call);// 强转        // 2.设置侦听        bt.setOnClickListener(new MyListener());    }    class MyListener implements OnClickListener {        // 按钮被点击时,此方法被调用        public void onClick(View v) {            //获取用户输入的号码                     EditText et = (EditText) findViewById(R.id.et_phone);            String phone = et.getText().toString();            //我们需要告诉系统,我们的动作,我要打电话            //创建意图对象            Intent intent = new Intent();            //把动作封装至意图当中            intent.setAction(Intent.ACTION_CALL);            //设置打给谁            intent.setData(Uri.parse("tel:"+ phone));            //把动作告诉系统            startActivity(intent);        }    }}

第三:需要获取打电话的权限

#android中凡是涉及到打电话(需要扣费),个人隐私等操作,都需要获取权限

权限添加

0 0