Android Stuidio —— 一键拨号

效果


代码

     关键代码

public void onClick(View view) 
{
	String num = "10086";
	Intent cn = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+num));
	startActivity(cn);
}


     完整代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="10086 一键拨号" />
</LinearLayout>
package com.example.application;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.button);
        if(button != null){
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String num = "10086";
                    Intent cn = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+num));
                    startActivity(cn);
                }
            });
        }
    }
}

关注

笔者 - jxd

微信公众号搜索 “码农总动员” 或 微信扫描下方二维码,了解更多你不知道的XX,O(∩_∩)O

在这里插入图片描述

### 如何在 Android Studio 中使用或配置记事本功能 在 Android Studio 中实现记事本功能,通常需要结合 SQLite 数据库、RecyclerView 和 Material Design 等组件来完成。以下将详细说明如何配置和实现一个简单的记事本功能。 #### 1. 创建项目 打开 Android Studio 并创建一个新的项目,选择 **Empty Activity** 模板。确保项目的最低 SDK 版本支持所需的功能[^1]。 #### 2. 配置 Git 仓库 为了更好地管理代码版本,可以在项目中集成 Git。通过点击工具栏的 **VCS** 菜单,选择 **Create Git Repository** 来初始化 Git 仓库[^1]。这样可以方便后续将代码上传到 GitHub。 #### 3. 添加依赖项 在 `build.gradle` 文件中添加必要的依赖项,例如用于 Material Design 的 Floating Action Button 和 RecyclerView 组件: ```gradle dependencies { implementation 'com.google.android.material:material:1.6.0' implementation 'androidx.recyclerview:recyclerview:1.2.1' } ``` #### 4. 设计用户界面 在布局文件中定义主界面,包含一个 RecyclerView 用于显示笔记列表,以及一个 Floating Action Button 用于添加新笔记: ```xml <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view_notes" android:layout_width="match_parent" android:layout_height="match_parent" /> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab_add_note" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="16dp" android:contentDescription="Add Note" /> </androidx.coordinatorlayout.widget.CoordinatorLayout> ``` 注意:如果出现类似 `fab_add_note <com.google.android.material.floatingactionbutton.FloatingActionButton>: No speakable text present` 的警告,可以通过设置 `android:contentDescription` 属性解决[^2]。 #### 5. 实现数据库功能 使用 SQLite 数据库存储笔记内容。创建一个继承自 `SQLiteOpenHelper` 的类来管理数据库操作: ```java public class NotesDatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "notes.db"; private static final int DATABASE_VERSION = 1; public NotesDatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String CREATE_NOTES_TABLE = "CREATE TABLE notes (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, content TEXT)"; db.execSQL(CREATE_NOTES_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS notes"); onCreate(db); } } ``` #### 6. 开发核心功能 - **添加笔记**:当用户点击 Floating Action Button 时,弹出一个对话框让用户输入笔记标题和内容,并将其保存到数据库中。 - **显示笔记列表**:从数据库中读取所有笔记,并通过 RecyclerView 显示在界面上。 - **删除笔记**:为每个笔记项添加删除按钮,允许用户删除不再需要的笔记。 #### 7. 测试与优化 运行应用并测试所有功能是否正常工作。可以参考已有的开源项目进行学习和改进,例如提供的 GitHub 仓库链接[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

信必诺

嗨,支持下哥们呗。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值