• android 一个简单的拨打电话程序


    我的这个电话拨打程序分一下几个部分,

    1、新建电话拨打程序CallPhone。

    2、节目UI设计。

    我的节目设计简单如下所示:

    UI界面对应的代码如下:

    <?xml version="1.0" encoding="utf-8"?>
    <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/phoneId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="2dp"
            android:textSize="16dp"
            android:textColor="#00000000"/>
        <Button 
            android:id="@+id/buttonId"
            android:layout_height="wrap_content"
            android:layout_width="100dp"
            android:text="拨打电话"/>
    
    </LinearLayout>
    View Code

    3、主程序对应的代码如下:

    package com.mr.cheng;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class CallPhoneActivity extends Activity implements OnClickListener{
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Button buttonId = (Button) findViewById(R.id.buttonId);
            buttonId.setOnClickListener(this);
            
        }
        public void callPhone(){
             EditText phoneText = (EditText) findViewById(R.id.phoneId);
             if(phoneText.getText().toString() != null){
                 Intent phoneIntent = new Intent("android.intent.action.CALL",
                           Uri.parse("tel:" + phoneText.getText().toString()));
                 startActivity(phoneIntent);
             } else{
                 Toast.makeText(CallPhoneActivity.this, "不能输入为空", Toast.LENGTH_LONG).show();
               }
        }
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.buttonId:
                callPhone();
                break;
    
            default:
                break;
            }
        }
    }
    View Code

    4、拨打电话必须添加拨打电话权限。

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.mr.cheng"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk android:minSdkVersion="14" />
        <!-- 添加拨打电话权限 -->
        <uses-permission android:name="android.permission.CALL_PHONE"/>
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:label="@string/app_name"
                android:name=".CallPhoneActivity" >
                <intent-filter >
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    View Code

    5、页面运行效果如下边图所示:

  • 相关阅读:
    UML第一次编程作业
    css文本属性
    css文字属性
    Qobject设置对象名称和属性
    m3u8
    easyUI tree jQuery
    easyUI layout
    正则表达式
    Spring mvc 数据验证框架注解
    blur和click事件的先后顺序问题
  • 原文地址:https://www.cnblogs.com/chengAddress/p/4444488.html
Copyright © 2020-2023  润新知