入门Android,使用AndroidStudio进行开发创建第一个APP,进行打电话

本人从昨天开始正式开始学习 Android 这门语言,要学这个,先要有Java基础,恰好我已经达标了,先说说我的学习过程

首先,我下载了Android Studio作为我的开发工具,Android Studio可以在这个网址进行下载

Android Studio下载

对于Android Studio,刚开始我觉得并不是那么好用,风格有点像IDEA的那种类型,因为本人用Eclipse用习惯了,再加上我看的教程视频也是用Eclipse进行开发的,关于AndroidStudio的安装不多解释了,直接点next就行了,期间需要用到的SDK什么的都会帮你下载下来,废话不多说,先整一个APP来刷一下成就感.

打开Android Studio,点击Start new Project大概就是这个,创建一个新项目

选择Empty Activity的模板进行创建

填写基本项目的信息,创建项目

扫描二维码关注公众号,回复: 10525257 查看本文章

将查看项目的方式从Android改成Project

然后就可以看到 在app下的res下的layout文件夹中有个activity_main.xml文件

这是app的布局文件,我们点开后可以自由的进行布局

会出现一个窗口 可以进行清晰的布局,如果没有的话则点击preview进行打开,组件面板在palette中打开后就可以进行随意的拖动放置组件了

先创建一个Text中的Phone,以及一个Button按钮

然后在代码中将Button按钮的android:text属性的值改为拨打电话

两个组件内都应该有一条android:id属性 用来给此组件命名@代表R.java文件,在AndroidStudio中 R文件找不到

里面配置了很多东西

我的布局代码是这样的

<!-- 拨打电话的组件一个文本框和一个按钮 -->
    <EditText
        android:id="@+id/phoneText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginRight="0dp"
        android:ems="10"
        android:inputType="phone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:id="@+id/phoneButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="@string/phoneButton"
        app:layout_constraintEnd_toEndOf="@+id/phoneText"
        app:layout_constraintTop_toBottomOf="@+id/phoneText" />

我给文本框命名为phoneText 按钮命名为phoneButton

UI弄好了接下来就要进行实际操作了,打开MainActivity.java文件

先创建一个判断是否为空的方法

private boolean isEmpty(String str) {
        return str.length() == 0 || null == str;
}

在onCreate方法中写以下代码进行拨打电话的操作

//获取Button
        Button phoneButton = findViewById(R.id.phoneButton);
        //给Butotn设置监听
        phoneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取Text的值
                EditText phoneText = findViewById(R.id.phoneText);
                String data = phoneText.getText().toString().trim();
                //判断值是否为空
                if (isEmpty(data)) {
                    //弹出提示框
                    Toast.makeText(MainActivity.this, "号码不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                //进行打电话
                //创建意图
                Intent intent = new Intent();
                //设置行为
                intent.setAction(Intent.ACTION_CALL);
                //设置执行行为的数据
                intent.setData(Uri.parse("tel:"+data));
                //开始执行意图
                startActivity(intent);

            }
        });

然后需要在清单文件中申请文件

清单文件在 app/src/main中 AndroidManifest.xml 在根中添加以下代码 进行申请打电话的权限

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

最后 点击run-run'app'进行运行app,如果没有创建AVD 则需要创建AVD(android模拟器)在Android的右上角有个类似于手机的图标叫AVD Manager进行创建

完成之后,运行进行打电话程序如果直接崩掉的话则证明没有给权限,需要在虚拟机中给对应的应用权限

好了 这就是我的第一个APP

发布了38 篇原创文章 · 获赞 23 · 访问量 9084

猜你喜欢

转载自blog.csdn.net/qq_41806966/article/details/89530530