做的比较简陋 一个编辑框 一个按钮


我们在layout中创建一个activity_main.XML 在里面添加一个线性垂直布局 一个编辑框 和一个按钮

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >  <EditText     android:id="@+id/editText"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    />  <Button       android:id="@+id/btn_call"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="拨号"      /></LinearLayout>

然后就是MainActivity.java

package com.example.myphone;import android.net.Uri;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity {private Button btn_call;private EditText editText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);/* * 获取拨打电话的按钮 * */btn_call=(Button) findViewById(R.id.btn_call);/* * 获取编辑框 * */editText=(EditText) findViewById(R.id.editText);/* * 对拨打电话按钮添加点击事件 * */btn_call.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub/* * 通过隐式intent打开拨号的activity * 其中android电话的intent-filter可以通过百度找到 * */Intent intent=new Intent();intent.setAction("android.intent.action.CALL");intent.addCategory(Intent.CATEGORY_DEFAULT);intent.setData(Uri.parse("tel:"+editText.getText().toString()));startActivity(intent);}});}}

如果你就写了这些 就运行 会发现手机提示错误 打开logcat会发现这个错误

仔细读读 发现是权限的问题。这是android为了安全考虑。我们还需要在AndroidManifest中配置一下打电话的权限。

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.myphone"    android:versionCode="1"    android:versionName="1.0" ><!-- 配置打电话的权限 -->    <uses-permission         android:name="android.permission.CALL_PHONE"        />    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="18" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.myphone.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

就到这里结束了 下面看看运行结果


更多相关文章

  1. Android--Intent的使用
  2. Android(安卓)studio 升级失败 connection failed的解决办法
  3. 腾讯云-视频直播(android集成)
  4. Android开发20——单个监听器监听多个按钮点击事件
  5. Android自启动应用添加和限制
  6. Android(安卓)ContentProvider简介
  7. Android(安卓)KEYCODE键值对应大全
  8. [Android(安卓)Pro] service中显示一个dialog 或者通过windowman
  9. Android(安卓)10动态申请读写权限

随机推荐

  1. 【Android】Activity遮罩效果的实现
  2. Android(安卓)APP 版本更新通知代码
  3. [置顶] Android加载数据过程中的菊花显示
  4. Android开发学习笔记整理(16)-网络请求
  5. 【转】Android(安卓)SDK版本与API Level
  6. Android(安卓)添加快捷方式
  7. android发送json并解析返回json
  8. Android为HttpClient设置证书(用户名和密
  9. android 与其他程序交互(一)
  10. [Android] ListView 结合SimpleAdapter使