我正在开发具有电话号码的ContactUs页面的Android应用程序。我在xml文件中给出了电话号码,如下所示:
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="1-869-270-9099"
android:textSize="11sp"
android:textColor="#104082"
android:textStyle="normal"
android:layout_width="wrap_content"
android:id="@+id/textView4"
android:layout_x="72dp"
android:layout_y="160dp"/>
我创建了AlertDialog
,点击了电话号码后,系统会以编程方式显示提醒dialog
,如下所示:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case (R.id.textView4):
Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to Call?");
builder.setCancelable(false);
builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Do Calling a Number
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
return super.onCreateDialog(id);
}
public void onClick(View v) {
switch(v.getId()){
case (R.id.textView4):
showDialog(R.id.textView4);
break;
}
}
我的问题是在onClick
“PositiveButton”方法中实现“拨打电话号码”功能。请帮助我如何获得一个带有该号码的DialPad带有SampleCode / Links的xml
文件。
答案 0 :(得分:13)
可能是这样的:
Intent call = new Intent(Intent.ACTION_DIAL);
call.setData(Uri.parse("tel:" + findViewByid(R.id.textView4).getText());
startActivity(call);
答案 1 :(得分:0)
在onClick
的{{1}}内,只需写下:
button
注意:强>
不要忘记在Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
dial.setData(Uri.parse("tel:"+dial_number));
startActivity(dial);
文件中添加此权限:
AndroidManifest.xml
答案 2 :(得分:0)
你可以直接启动一个意图来获得带有xml文件中提到的数字的拨号盘。就像这样:
String contact=findViewById(R.id.textView4).getText().toString();
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + contact));
startActivity(callIntent);