直接新建一个工程,
先画图形----再写点击事件---->然后给权限--->测试
方法一:
activity_main.xml 配置如下
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.admin.onclick.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/bt_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="322dp" />
<EditText
android:hint="请输入电话号码"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="53dp"
android:id="@+id/et_phone"
android:layout_above="@+id/bt_call"
android:layout_centerHorizontal="true" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
package com.example.admin.onclick;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText et_phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_phone = (EditText) findViewById(R.id.et_phone);
Button bt_call = (Button) findViewById(R.id.bt_call);
//设点事件
bt_call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取输入的电话号码
String phone = et_phone.getText().toString().trim();
//创建打电话的意图
Intent intent = new Intent();
//设拔打电话的动作
intent.setAction(Intent.ACTION_CALL);
//设打电话号码
intent.setData(Uri.parse("tel:" + phone));
//开启打电话的意图
startActivity(intent);
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.onclick">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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>
<!--给打电话权限-->
<uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest>
可参考:https://www.cnblogs.com/ahu-lichang/p/6567945.html
以上示例本人均测试过可以正常运行。