手机拨号器实现

2022-07-26,

手机拨号器实现

  • 下载安装Android studio
      • 代码展示
      • 拨打10086

下载安装Android studio

建议下载这个不用eclipse,因为下载谷歌sdk时发生跳墙。
推荐教程
android studio安装教程

代码展示

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_horizontal_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:src="@drawable/sky"
            android:layout_weight="7"/>

        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_marginTop="20dp"
            android:layout_gravity="center"
            android:layout_weight="2"
            android:ems="10"
            android:focusable="false"
            android:hint="请输入号码" >

            <requestFocus />
        </EditText>

        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_marginTop="20dp"
            android:layout_weight="12" >

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1" >

                <ImageButton
                    android:id="@+id/imageButton1"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@drawable/bg_alibuybutton"
                    android:onClick="click"
                    android:src="@drawable/n1"
                    android:tag="1" />

                <ImageButton
                    android:id="@+id/imageButton2"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@drawable/bg_alibuybutton"
                    android:onClick="click"
                    android:src="@drawable/n2"
                    android:tag="2" />

                <ImageButton
                    android:id="@+id/imageButton3"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@drawable/bg_alibuybutton"
                    android:onClick="click"
                    android:src="@drawable/n3"
                    android:tag="3" />

            </TableRow>

            <TableRow
                android:id="@+id/tableRow2"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1" >

                <ImageButton
                    android:id="@+id/imageButton4"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@drawable/bg_alibuybutton"
                    android:onClick="click"
                    android:src="@drawable/n4"
                    android:tag="4" />

                <ImageButton
                    android:id="@+id/imageButton5"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@drawable/bg_alibuybutton"
                    android:onClick="click"
                    android:src="@drawable/n5"
                    android:tag="5" />

                <ImageButton
                    android:id="@+id/imageButton6"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@drawable/bg_alibuybutton"
                    android:onClick="click"
                    android:src="@drawable/n6"
                    android:tag="6" />

            </TableRow>

            <TableRow
                android:id="@+id/tableRow3"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1" >

                <ImageButton
                    android:id="@+id/imageButton7"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@drawable/bg_alibuybutton"
                    android:onClick="click"
                    android:src="@drawable/n7"
                    android:tag="7" />

                <ImageButton
                    android:id="@+id/imageButton8"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@drawable/bg_alibuybutton"
                    android:onClick="click"
                    android:src="@drawable/n8"
                    android:tag="8" />

                <ImageButton
                    android:id="@+id/imageButton9"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@drawable/bg_alibuybutton"
                    android:onClick="click"
                    android:src="@drawable/n9"
                    android:tag="9" />

            </TableRow>

            <TableRow
                android:id="@+id/tableRow4"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1" >

                <ImageButton
                    android:id="@+id/imageButton10"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@drawable/bg_alibuybutton"
                    android:onClick="click"
                    android:src="@drawable/star"
                    android:tag="*" />

                <ImageButton
                    android:id="@+id/imageButton11"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@drawable/bg_alibuybutton"
                    android:onClick="click"
                    android:src="@drawable/n0"
                    android:tag="0" />

                <ImageButton
                    android:id="@+id/imageButton12"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@drawable/bg_alibuybutton"
                    android:onClick="click"
                    android:src="@drawable/sharp"
                    android:tag="#" />

            </TableRow>

            <TableRow
                android:id="@+id/tableRow5"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1" >

                <ImageButton
                    android:id="@+id/imageButton13"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@drawable/bg_alibuybutton1"
                    android:onClick="click"
                    android:src="@drawable/add_n"
                    android:tag="+" />

                <ImageButton
                    android:id="@+id/imageButton14"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@drawable/bg_alibuybutton1"
                    android:onClick="dial"
                    android:src="@drawable/dialpad"
                    android:tag="dial" />

                <ImageButton
                    android:id="@+id/imageButton15"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@drawable/bg_alibuybutton1"
                    android:onClick="del"
                    android:src="@drawable/delete_n"
                    android:tag="del" />


            </TableRow>
        </TableLayout>
    </LinearLayout>


</RelativeLayout>

MainActivity

package com.example.call;

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.widget.EditText;
import android.widget.Toast;

import com.example.call.R;

public class MainActivity extends Activity {

    EditText et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et = (EditText)this.findViewById(R.id.editText1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void click(View view){
        String str = et.getText().toString();
        str += view.getTag().toString();
        et.setText(str);
    }

    public void dial(View view){
        String str = et.getText().toString();
        if((str != null) && !(str.trim().equals(""))){
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + str));
            startActivity(intent);
        }else{
            Toast.makeText(MainActivity.this, "请拨打号码按钮", Toast.LENGTH_LONG).show();
        }
    }

    public void del(View view){
            String str = et.getText().toString();
            str = str.substring(0, str.length() - 1);
            et.setText(str);
        }

}


拨打10086

插入代码在androidmainfest.xml

 <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>

最后要使得手机打开开发者模式。

本文地址:https://blog.csdn.net/m0_45020051/article/details/110565783

《手机拨号器实现.doc》

下载本文的Word格式文档,以方便收藏与打印。