说明
根据输入的联系人姓名,来删除该联系人的所有的信息。
效果
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入要删除的联系人名字"
/>
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
/>
</LinearLayout>
activity书写
package com.example.myapp;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MyActivity extends Activity {
private EditText name;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name = (EditText) findViewById(R.id.name);
Button delete = (Button) findViewById(R.id.delete);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String nameStr = name.getText().toString();
//先查询到name对应的rawcontactsid
ContentResolver cr = getContentResolver();
int re = cr.delete(ContactsContract.RawContacts.CONTENT_URI,
"display_name = ?",
new String[]{nameStr}
);
if (re > 0) {
Toast.makeText(MyActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
}
}
});
}
}
时间: 2024-11-05 10:03:00