Android开发入门之拨打电话
16lz
2021-01-24
第一步:新建一个Android工程命名为02.Phone目录结构如下图:
第二步:修改activity_main.xml布局文件代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/please_enter_phone_number" /> <EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="phone" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="call" android:text="@string/call" /></LinearLayout>
strings.xml:
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">02.Phone</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="please_enter_phone_number">请输入电话号码</string> <string name="call">呼叫此号码</string></resources>
第三步:编写MianActivity类:
package cn.leigo.phone;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.widget.EditText;public class MainActivity extends Activity {private EditText mPhoneEditText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mPhoneEditText = (EditText) findViewById(R.id.et_phone);}public void call(View v) {String number = mPhoneEditText.getText().toString();if (!TextUtils.isEmpty(number)) {Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ number));startActivity(intent);}}}
最后在AndroidManifest.xml文件中添加权限:
<uses-permission android:name="android.permission.CALL_PHONE" />
运行工程查看效果图:
更多相关文章
- Android(安卓)Studio 在命令行编译工程
- aar文件的libs有其他jar文件,使用时的注意点
- java更改android文件权限--待验证
- android关于手机和3.0版本以上平板去标题问题
- 2011-03-29 15:30 android用于打开各种文件的intent【原创】
- Android计算文件夹大小、文件大小单位转换、删除文件夹及其内容
- Intent打开各种类型文件
- Android(安卓)安装文件.apk反编译