Android开发之实现电话app(七)

call_phone_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/call_ed"
        android:hint="输入电话号码"
        android:inputType="phone"
        />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/call_bu"
        android:text="拨打电话"/>
</LinearLayout>

CallPhoneActivity.java

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

public class CallPhoneActivity extends AppCompatActivity {

    EditText callEd;
    Button callBu;

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

        //初始化UI
        callEd=findViewById(R.id.call_ed);
        callBu=findViewById(R.id.call_bu);

        //监听按钮点击事件
        callBu.setOnClickListener(new View.OnClickListener(){
           @Override
            public void onClick(View view){
                //获取号码
                String phone = callEd.getText().toString().trim();

                //创建意图(隐式意图)
                //操作:Intent.ACTION_DIAL
                //数据:Uri.parse("tel:" + phone)

                //Uri UriPhone=Uri.parse("tel:" + phone);
                // Intent dialIntent = new Intent(Intent.ACTION_DIAL, UriPhone);

                Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));

                //写法二
               Intent myIntent=new Intent();
               myIntent.setAction(Intent.ACTION_DIAL);
               myIntent.setData(Uri.parse("tel:" + phone));


                //启动意图
                startActivity(dialIntent);
            }
        });
    }
}

效果

 

 

猜你喜欢

转载自blog.csdn.net/qq_53376718/article/details/129738351