说明
将提供的联系人姓名修改为期待修改成的联系人姓名。
效果
布局
<?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/name1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入修改前的名字"
/>
<EditText
android:id="@+id/name2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入希望修改成的名字"
/>
<Button
android:id="@+id/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改"
/>
</LinearLayout>
activity书写
package com.ht.demo03;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
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 {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText name1 = (EditText) findViewById(R.id.name1);
final EditText name2 = (EditText) findViewById(R.id.name2);
Button update = (Button) findViewById(R.id.update);
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name1Str = name1.getText().toString();
String name2Str = name2.getText().toString();
ContentResolver cr = getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put("display_name", name2Str);
int re = cr.update(
ContactsContract.RawContacts.CONTENT_URI,
contentValues,
"display_name = ?",
new String[]{name1Str}
);
if (re > 0) {
Toast.makeText(MyActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
}
}
});
}
}
时间: 2024-12-22 03:24:40