xml中的代码实现
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title" />
<EditText
android:id="@+id/phonenumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入手机号码"
android:inputType="phone"
android:layout_below="@id/title"
/>
<Button
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="call"
android:text="拨打"
android:layout_below="@id/phonenumber"
/>
</RelativeLayout>
mainActivity中代码的实现
public class MainActivity extends Activity {
private EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit=(EditText)findViewById(R.id.phonenumber);
}
public void call(View v){
System.out.println("被点击了");
String num=edit.getText().toString();
Intent intent=new Intent();//创建一个意图
intent.setAction(intent.ACTION_CALL);//指定动作为拨打电话
intent.setData(Uri.parse("tel:"+num));//指定要播出的电话号码
startActivity(intent);
}
}
同时还需要给我们的应用程序添加一个可以拨打号码的权限
<!-- 添加拨打电话的权限 -->
<uses-permission
android:name="android.permission.CALL_PHONE"
/>
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。