删除,拨打电话,发送短信对话框

时间:2022-11-10 13:10:49

1-KehoutiActivity.java

删除,拨打电话,发送短信对话框删除,拨打电话,发送短信对话框
package com.example.lenovo.textapp4;

import android.app.AlertDialog;
import android.content.DialogInterface;
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.EditText;
import android.widget.Toast;

public class KehoutiActivity extends AppCompatActivity {


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




}


public void bt_1(View v)
{
AlertDialog alertDialog=new AlertDialog.Builder(this)
.setTitle("提示")
.setMessage("确定要删除吗?\n要删除,请点击“是”")
.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(KehoutiActivity.this, "已删除", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(KehoutiActivity.this, "未删除", Toast.LENGTH_SHORT).show();
}
})
.show();
}


public void bt_2(View v)
{
final View view=View.inflate(this, R.layout.shurudianhuahaoma, null);
et_1=(EditText)view.findViewById(R.id.et_1);
new AlertDialog.Builder(this)
.setTitle("请输入电话号码")
.setView(view)
.show();
}
public String getphone()
{
String phone=et_1.getText().toString().trim();
if (phone.trim().length()==0)
{
Toast.makeText(KehoutiActivity.this, "请正确填写电话号码", Toast.LENGTH_SHORT).show();
return null;
}
return phone;
}

public void bt_11(View v)
{
String phone=getphone();
if (phone==null) return;
Intent intent=new Intent(Intent.ACTION_DIAL);
Uri uri=Uri.parse("tel:" + phone);

intent.setData(uri);
startActivity(intent);
}
public String getMessage()
{
String message = et_1.getText().toString().trim();
if (message.length()==0)
{
Toast.makeText(KehoutiActivity.this, "请正确填写电话号码", Toast.LENGTH_SHORT).show();
return null;

}
return message;
}

public void bt_22(View v)
{
String message = getMessage();
if (message == null) return;
Uri smsToUri = Uri.parse("smsto:message");
Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
intent.setData(Uri.parse("smsto:"+message));
startActivity(intent);
}

}
View Code

 

 

2-activity_kehouti.xml

删除,拨打电话,发送短信对话框删除,拨打电话,发送短信对话框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingBottom
="@dimen/activity_vertical_margin"
android:paddingLeft
="@dimen/activity_horizontal_margin"
android:paddingRight
="@dimen/activity_horizontal_margin"
android:paddingTop
="@dimen/activity_vertical_margin"
tools:context
="com.example.lenovo.textapp4.KehoutiActivity"
android:orientation
="vertical">
<Button
android:layout_width="match_parent"
android:layout_height
="wrap_content"
android:text
="删除"
android:onClick
="bt_1"/>
<Button
android:layout_width="match_parent"
android:layout_height
="wrap_content"
android:text
="跳转"
android:onClick
="bt_2"/>

</LinearLayout>
View Code

 

 

3-shuruhaoma.xml

删除,拨打电话,发送短信对话框删除,拨打电话,发送短信对话框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="vertical" android:layout_width="match_parent"
android:layout_height
="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height
="wrap_content"
android:id
="@+id/et_1"
android:inputType
="phone"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height
="wrap_content"
android:orientation
="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height
="wrap_content"
android:layout_weight
="1"
android:text
="向此号码拨出电话"
android:onClick
="bt_11"/>
<Button
android:layout_width="wrap_content"
android:layout_height
="wrap_content"
android:layout_weight
="1"
android:text
="向此号码发送短信"
android:onClick
="bt_22"/>
</LinearLayout>

</LinearLayout>
View Code

 

 

结果

删除,拨打电话,发送短信对话框删除,拨打电话,发送短信对话框删除,拨打电话,发送短信对话框

删除,拨打电话,发送短信对话框删除,拨打电话,发送短信对话框删除,拨打电话,发送短信对话框删除,拨打电话,发送短信对话框