【编程新实务】Lab3 Android手机拨号器开发

项目地址:

github项目地址

实验目的:

安装配置好Android开发环境,并使用其开发Android手机拨号器。

实验过程

本实验是一个验证实验,主要是为了配置好Android开发环境。

刚开始在idea配置安卓环境,但是新建的项目他就是报错,网上找了各种原因,说是没下载对应安卓版本sdk,看了一下也下载了,然后又是Gradle的问题,也各种下载了,为了配置环境去下载了各种资源,还花了6.9买了一个百度网盘一天下载加速券,结果还是没整好。。。

。。。然后我放弃了

索性直接下载Android
推荐看这篇博客安装

当我安好后,意外的发现idea里面下载的android的sdk能直接用

主要需要把实验文档里的两个地方代码填进去就行

1.MainActivity类

package com.example.myapplication;
import android.content.Intent;
import android.net.Uri;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    EditText et;

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

    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();
        if((str != null) && !str.trim().equals("")){
            str = str.substring(0,str.length() - 1);
            et.setText(str);
        }
    }


}

2.ativity_main.xml
里面用到很多图片素材,不加到对应路径下会报红

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_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" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            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_alibuybutton"
                    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_alibuybutton"
                    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_alibuybutton"
                    android:onClick="del"
                    android:src="@drawable/delete_n"
                    android:tag="del" />


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



用到的图片素材放的路径如下图
在这里插入图片描述
素材上传到微云,下载地址:https://share.weiyun.com/TjCqbzkG

另外一个需要添加的文件:
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="activity_vertical_margin" />
    <dimen name="activity_horizontal_margin" />
</resources>

3.AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="codefriday自制拨号器"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

注意:一定要加上表示要一个拨号权限

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

总结

可以在AndroidStudio里面配置好一个虚拟机运行,也可以在build菜单下打包成apk发送到物理机安装测试,但一定注意打开通话权限,否则点拨号会闪退!!!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值