AndroidStudioのケース-電話のダイヤルを実現

目次

1つは、コード構成です。

1.プロジェクトを作成します

フローチャート
ここに画像の説明を挿入
ここに画像の説明を挿入
ここに画像の説明を挿入
ここに画像の説明を挿入

2.コードを追加します

レイアウトを変更する
ここに画像の説明を挿入
完全なコードをレイアウトする

<?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:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="电话拨号"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

画像を挿入
ここに画像の説明を挿入

activity_main_xmlファイルレイアウトの
ここに画像の説明を挿入
完全なコード

<?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:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="电话拨号"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入电话号码"
        android:inputType="number"
        android:id="@+id/phoneNum"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/call"
            android:layout_centerInParent="true" 
            android:id="@+id/call_btn"/>
    </RelativeLayout>
</LinearLayout>

エフェクト表示
ここに画像の説明を挿入
mainactivity.javaファイル

 EditText phoneNum;
    ImageButton call_btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        phoneNum=(EditText) findViewById(R.id.phoneNum);
        call_btn=(ImageButton) findViewById(R.id.call_btn);
       call_btn.setOnClickListener(new View.OnClickListener() {
    
    
           @Override
           public void onClick(View v) {
    
    
               Intent intent=new Intent();
               intent.setAction(Intent.ACTION_CALL);
               intent.setData(Uri.parse("tel:"+phoneNum.getText()));
               startActivity(intent);

画像
ここに画像の説明を挿入
実行コード
ここに画像の説明を挿入
ダイヤルしようとすると、エラーが発生します。対応する権限を設定する必要があります
ここに画像の説明を挿入

3.許可リクエスト

Androidmainfest.xmlファイル

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

ここに画像の説明を挿入

Android 6.0以降では、自分で手動で権限を付与する必要があります。

申請許可申請
バージョン番号判定方法

protected boolean shouldAskPermissions(){
    
    
    return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
}

許可の申請方法

protected void askPermissions() {
    
    
    String[] permissions = {
    
    
            "android.permission.CALL_PHONE"
    };
    int requestCode = 200;
    requestPermissions(permissions, requestCode);
}

onCreateを呼び出す

if(shouldAskPermissions()){
    
    
    askPermissions();
}

ここに画像の説明を挿入

コードを追加する許可を要求するときは、プログラムが実行されていることを確認してください。実行されていない場合、コードの追加時にエラーが報告されます。

第二に、効果のデモンストレーション

ここに画像の説明を挿入
ここに画像の説明を挿入

ここに画像の説明を挿入

番号をランダムに入力すると、プロンプトは空白の番号になります。

おすすめ

転載: blog.csdn.net/QWERTYzxw/article/details/115094179