Android开发实验---通讯录
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验报告
通讯录
姓名:
学号:
课程名称:移动应用开发
所在学院:信息科学与工程学院
专业班级:计算机
任课教师:
主要功能分析:
1、增加、删除、编联系人
点击通信录界面中的增加按钮,入增加联系人面。
输入联系人的基本信息,并可根据用户需求增加个性化信息如头像、姓名、手机号码、办室电话、家庭电话、职务职称、单位名称、地址、邮政编码、Email、其他联系方式、备注这些信息,击确认返回主界面。
点击通信录中一个已存在的联系人,进入联系人编辑界面,可修改系人的资料或进行删除联系人操作,完成后退回到主界面。
对列表中联系人的标记,点mnu键弹出功能界面上的删除按键也可进行删除。
还可以在菜单上选择删除全部联系人清空通讯录。
在删除联系人的过程中,系统将提示用户是否继续操作,若放弃操作,则系人信息将继续保存。
2、查找联系人
用户点击menu键打开底部菜单框,底部菜单框为查询系人提供入口,进入通讯录的缺省页面为联系人列表,在列表中看到所有联系人的姓名、电话息排列,用户点击查找按键输入联系人基本信息,,通讯录显所有符合查询条件的联系人列表,用户选择一个联系人进入联系人基本信息页面进行其他操作;查询完成,用户按返回键返回主界面。
3、通功能
用户在通录选择联系人进入联系详细信息界面,这时点击menu键打开通信功能框,选择打电话、发信息的功能进行操作。
4、菜单能
通过对menu按的点击,显示底部菜框,包含有增加查找、除、菜单、返回功能,菜单按键则包含显示所有、删除所有等实用功能。
增加联系人:
这个功能由ContactAdder类完成,具体实现不是比较麻烦,保存动作由ContentResolver类解决,但实现方式有所不同,可分为一次性批量增加与挨个增加。
由于界面设置繁琐,代码页过多,所以挑出其中的一部分进行说明
首先是DB数据库建库过程:
package com.xample.hivian.my_contact_manager.models.db;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.example.hivian.my_contact_manager.models.Contact;
import com.example.hivian.my_contact_manager.models.Sms;
import java.util.ArrayList;
import java.util.List;
public class DBHandler extends SQLiteOpenHelper {// Database Version
private static final int DB_VERSION = 1;// Database Name
private static final String DB_NAME = "DB";// Contacts table name
private static final String CONTACTS_TABLE = "contacts";//通讯录
private static final String SMS_TABLE = "sms";// Shops Table Columns names private static final String KEY_ID = "id";
private static final String KEY_IMAGE = "image";//图片
private static final String KEY_NAME = "name";//姓名
private static final String KEY_PHONE = "phone";//电话号码
private static final String KEY_EMAIL = "email";//邮箱
private static final String KEY_ADDRESS = "address";//联系人地址
private static final String KEY_SMS_HEADER = "sms_header";
private static final String KEY_SMS_CONTENT = "sms_content";
private static final String KEY_CONTACT_ID = "contact_id";
private static final String KEY_SMS_TYPE = "sms_type";
private static final String CONTACTS_TABLE_CREATE =
"CREATE TABLE " + CONTACTS_TABLE + " (" + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_IMAGE + " BLOB, " + KEY_NAME + " TEXT, " + KEY_PHONE + " TEXT, " +
KEY_EMAIL + " TEXT, " + KEY_ADDRESS + " TEXT)";
private static final String SMS_TABLE_CREATE =
"CREATE TABLE " + SMS_TABLE + " (" + KEY_ID + " INTEGER PRIMARY KEY, " +
KEY_SMS_HEADER + " TEXT, " + KEY_SMS_CONTENT + " TEXT, " +
KEY_CONTACT_ID + " INTEGER," + KEY_SMS_TYPE + " INTEGER)";
private static DBHandler dbInstance= null;
public static DBHandlergetInstance(Context context) {
if (dbInstance== null) {
dbInstance= new DBHandler(context, DB_NAME, DB_VERSION);
}
return dbInstance;
}
DBHandler(Context context, String dbName, int dbVersion) {
super(context, dbName, null, dbVersion);
}
@Override
public void onCreate(SQLiteDatabasedb) {
db.execSQL(CONTACTS_TABLE_CREATE);
db.execSQL(SMS_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabasedb, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + CONTACTS_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + SMS_TABLE);
// 重新创建表
onCreate(db);
}
//新增联系人
public void addContact(Contact contact) {
SQLiteDatabasedb = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_IMAGE, contact.getImage());
values.put(KEY_NAME, contact.getName());
values.put(KEY_PHONE, contact.getPhone());
values.put(KEY_EMAIL, contact.getEmail());
values.put(KEY_ADDRESS, contact.getAddress());
db.insert(CONTACTS_TABLE, null, values);
}
// Adding new sms
public void addSms(Smssms) {
SQLiteDatabasedb = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_SMS_HEADER, sms.getHeader());
values.put(KEY_SMS_CONTENT, sms.getContent());
values.put(KEY_CONTACT_ID, sms.getContactId());
values.put(KEY_SMS_TYPE, sms.getType());
db.insert(SMS_TABLE, null, values);
}
// Getting one contact
public Contact getContact(int id) {
SQLiteDatabasedb = this.getReadableDatabase();
Cursor cursor = db.query(CONTACTS_TABLE, new String[]
{ KEY_ID, KEY_IMAGE, KEY_NAME, KEY_PHONE, KEY_EMAIL, KEY_ADDRESS }, KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Contact contact = new Contact(cursor.getBlob(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5));
contact.setId(cursor.getInt(0));
cursor.close();
return contact;
}
// Getting one contact by name
public Contact getContactByName(String name) {
SQLiteDatabasedb = this.getReadableDatabase();
Cursor cursor = db.query(CONTACTS_TABLE, new String[]
{ KEY_ID, KEY_IMAGE, KEY_NAME, KEY_PHONE, KEY_EMAIL, KEY_ADDRESS }, KEY_NAME + "=?", new String[] { name }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Contact contact = new Contact(cursor.getBlob(1), cursor.getString(2),
cursor.getString(3), cursor.getString(4), cursor.getString(5));
contact.setId(cursor.getInt(0));
cursor.close();
return contact;
}
// Getting one contact by phone
public Contact getContactByPhone(String phone) {
SQLiteDatabasedb = this.getReadableDatabase();
Cursor cursor = db.query(CONTACTS_TABLE, new String[]
{ KEY_ID, KEY_IMAGE, KEY_NAME, KEY_PHONE, KEY_EMAIL, KEY_ADDRESS }, KEY_PHONE + "=?", new String[] { phone }, null, null, null, null);
if (!cursor.moveToFirst())
return null;
Contact contact = new Contact(cursor.getBlob(1), cursor.getString(2),
cursor.getString(3), cursor.getString(4), cursor.getString(5));
contact.setId(cursor.getInt(0));
cursor.close();
return contact;
}
// Getting one sms by contact id
public List<Sms>getSmsByContactId(Integer id) {
SQLiteDatabasedb = this.getReadableDatabase();
Cursor cursor = db.query(SMS_TABLE, new String[]
{ KEY_ID, KEY_SMS_HEADER, KEY_SMS_CONTENT, KEY_CONTACT_ID,
KEY_SMS_TYPE },
KEY_CONTACT_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null);
List<Sms>allSms = new ArrayList<>();
if (cursor.moveToFirst()) {
do {
Smssms = new Sms();
sms.setId(Integer.parseInt(cursor.getString(0)));
sms.setHeader(cursor.getString(1));
sms.setContent(cursor.getString(2));
sms.setContactId(cursor.getInt(3));
sms.setType(cursor.getInt(4));
allSms.add(sms);
} while (cursor.moveToNext());
}
cursor.close();
return allSms;
}
// Getting all contacts
public List<Contact>getAllContacts() {
String selectQuery = "SELECT * FROM " + CONTACTS_TABLE; SQLiteDatabasedb = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
List<Contact>allContacts = new ArrayList<>();
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setId(Integer.parseInt(cursor.getString(0)));
contact.setImage(cursor.getBlob(1));
contact.setName(cursor.getString(2));
contact.setPhone(cursor.getString(3));
contact.setEmail(cursor.getString(4));
contact.setAddress(cursor.getString(5));
allContacts.add(contact);
} while (cursor.moveToNext());
}
cursor.close();
return allContacts;
}
// Getting all sms
public List<Sms>getAllSms() {
String selectQuery = "SELECT * FROM " + SMS_TABLE; SQLiteDatabasedb = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
List<Sms>allSms = new ArrayList<>();
if (cursor.moveToFirst()) {
do {
Smssms = new Sms();
sms.setId(Integer.parseInt(cursor.getString(0)));
sms.setHeader(cursor.getString(1));
sms.setContent(cursor.getString(2));
sms.setContactId(cursor.getInt(3));
sms.setType(cursor.getInt(4));
allSms.add(sms);
} while (cursor.moveToNext());
}
cursor.close();
return allSms;
}
// Getting all sms from contact
public List<Sms>getAllSmsFromContact(int id) {
String selectQuery = "SELECT * FROM " + SMS_TABLE + " WHERE " + KEY_CONTACT_ID + " = " + id;
SQLiteDatabasedb = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
List<Sms>allSms = new ArrayList<>();
if (cursor.moveToFirst()) {
do {
Smssms = new Sms();
sms.setId(Integer.parseInt(cursor.getString(0)));
sms.setHeader(cursor.getString(1));
sms.setContent(cursor.getString(2));
sms.setContactId(cursor.getInt(3));
sms.setType(cursor.getInt(4));
allSms.add(sms);
} while (cursor.moveToNext());
}
cursor.close();
return allSms;
}
// Getting contacts count
public Integer getContactsCount() {
String countQuery = "SELECT * FROM " + CONTACTS_TABLE; SQLiteDatabasedb = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
return cursor.getCount();
}
// Checking duplicates in table
public boolean isDuplicate(DBHandlerdb, String name) {
List<Contact> contacts = db.getAllContacts();
for (Contact cont : contacts) {
if (cont.getName().equals(name))
return true;
}
return false;
}
// Updating a contact
public Integer updateContact(Contact contact) {
SQLiteDatabasedb = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_IMAGE, contact.getImage());
values.put(KEY_NAME, contact.getName());
values.put(KEY_PHONE, contact.getPhone());
values.put(KEY_EMAIL, contact.getEmail());
values.put(KEY_ADDRESS, contact.getAddress());
return db.update(CONTACTS_TABLE, values, KEY_ID + " = ?", new String[]{String.valueOf(contact.getId())});
}
// Updating a sms
public Integer updateSms(Smssms) {
SQLiteDatabasedb = this.getWritableDatabase(); ContentValues values = new ContentValues();
values.put(KEY_SMS_HEADER, sms.getHeader());
values.put(KEY_SMS_CONTENT, sms.getContent());
values.put(KEY_CONTACT_ID, sms.getContactId());
values.put(KEY_SMS_TYPE, sms.getType());
return db.update(SMS_TABLE, values, KEY_ID + " = ?",
new String[]{String.valueOf(sms.getId())});
}
// Deleting a contact
public void deleteContact(Contact contact) { SQLiteDatabasedb = this.getWritableDatabase();
db.delete(CONTACTS_TABLE, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getId()) });
}
// Deleting a contact
public void deleteSms(Smssms) {
SQLiteDatabasedb = this.getWritableDatabase();
db.delete(SMS_TABLE, KEY_ID + " = ?",
new String[] { String.valueOf(sms.getId()) });
}
// Deleting all contacts
public void deleteAllContacts(DBHandlerdb) {
List<Contact> contacts = db.getAllContacts();
for (Contact cont : contacts) {
db.deleteContact(cont);
}
}
// Deleting all sms
public void deleteAllSms(DBHandlerdb) {
List<Sms>allSms = db.getAllSms();
for (Smssms : allSms) {
db.deleteSms(sms);
}
}
}
界面切换:
package com.example.hivian.my_contact_manager.views.activities;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import com.example.hivian.my_contact_manager.R;
import java.util.Timer;
import java.util.TimerTask;
public class SplashActivity extends Activity {
private static final long DELAY = 1000;
private boolean scheduled = false;
private Timer splashTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(yout.activity_splash);
this.getWindow().setFlags(youtParams.FLAG_FULLSCREEN, youtParams.FLAG_FULLSCREEN);
splashTimer= new Timer();
splashTimer.schedule(new TimerTask()
{
@Override
public void run()
{
Intent intent = new Intent(SplashActivity.this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
overridePendingTransition(0, 0);
SplashActivity.this.finish();
}
}, DELAY);
scheduled = true;
}
@Override
protected void onDestroy() {
super.onDestroy();
if (scheduled) {
splashTimer.cancel();
}
splashTimer.purge();
}
}
按钮设计:
package com.example.hivian.my_contact_manager.views.fragments; import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import .Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import youtInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.Fragment;
import com.example.hivian.my_contact_manager.R;
import com.example.hivian.my_contact_manager.models.Contact;
import com.example.hivian.my_contact_manager.models.Sms;
import com.example.hivian.my_contact_manager.models.db.DBHandler;
import com.example.hivian.my_contact_manager.utilities.BitmapUtility;
import com.example.hivian.my_contact_manager.views.activities.ContactEditionActivity; import com.example.hivian.my_contact_manager.views.activities.ContactSmsActivity;
import com.example.hivian.my_contact_manager.views.activities.MainActivity;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionMenu;
import com.oguzdev.circularfloatingactionmenu.library.SubActionButton;
import java.util.List;
public class ContactInfoFragment extends Fragment implements View.OnClickListener, View.OnTouchListener {
private TextView name;
private TextView phone;
public FloatingActionMenu actionMenu;
private DBHandler db;
@Nullable
@Override
public View onCreateView(LayoutInflaterinflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(yout.fragment_contact_info, container, false);
setHasOptionsMenu(true);
ActionBar ab = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (ab != null) {
ab.setTitle("Options");
ab.setDisplayHomeAsUpEnabled(true);
}
db= DBHandler.getInstance(getActivity());
Contact receivedContact = (Contact) getArguments().getSerializable("contact");
if (receivedContact != null) {
ImageViewimageView = (ImageView) view.findViewById(_image);
name = (TextView) view.findViewById(_name);
phone = (TextView) view.findViewById(_phone);
TextView email = (TextView) view.findViewById(_email);
TextView address = (TextView) view.findViewById(_address);
if (receivedContact.getImage() != null) {
Bitmap imageBm = BitmapUtility.getImage(receivedContact.getImage());
imageView.setImageBitmap(imageBm);
}
name.setText(receivedContact.getName());
phone.setText(receivedContact.getPhone());
if (receivedContact.getEmail().equals("")) {
email.setTypeface(null, Typeface.ITALIC);
email.setTextColor(Color.GRAY);
email.setTextSize(PLEX_UNIT_DIP, 18f);
email.setText(R.string.placeholder_none);
}
else
email.setText(receivedContact.getEmail());
if (receivedContact.getAddress().equals("")) {
address.setTypeface(null, Typeface.ITALIC);
address.setTextColor(Color.GRAY);
address.setTextSize(PLEX_UNIT_DIP, 18f);
address.setText(R.string.placeholder_none);
}
else
address.setText(receivedContact.getAddress());
}
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { initInfoMenu(view);
ScrollViewscrollView = (ScrollView) view.findViewById(R.id.scrollview_contact_info); scrollView.setOnTouchListener(this);
super.onViewCreated(view, savedInstanceState);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
actionMenu.close(true);
return false;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.itemId1:
toSmsManager();
break;
case R.id.itemId2:
callContact();
break;
case R.id.itemId3:
editContact();
break;
case R.id.itemId4:
deleteContact();
break;
}
actionMenu.close(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflaterinflater) {
inflater.inflate(R.menu.menu_main, menu);
super.onCreateOptionsMenu(menu,inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
actionMenu.close(true);
getFragmentManager().popBackStack();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void initInfoMenu(View view) {
ImageView menuIcon1 = new ImageView(getActivity());
ImageView menuIcon2 = new ImageView(getActivity());
ImageView menuIcon3 = new ImageView(getActivity());
ImageView menuIcon4 = new ImageView(getActivity());
menuIcon1.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_message_black_24dp, null));
menuIcon2.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_call_black_24dp, null));
menuIcon3.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_mode_edit_black_24dp, null));
menuIcon4.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_delete_black_24dp, null));
menuIcon1.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.circle_opacity, null));
menuIcon2.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.circle_opacity, null));
menuIcon3.setBackground(ResourcesCompat.getDrawable(getResources(),
R.drawable.circle_opacity, null));
menuIcon4.setBackground(ResourcesCompat.getDrawable(getResources(),
R.drawable.circle_opacity, null));
menuIcon1.setColorFilter(ContextCompat.getColor(getActivity(), android.R.color.white));
menuIcon2.setColorFilter(ContextCompat.getColor(getActivity(), android.R.color.white));
menuIcon3.setColorFilter(ContextCompat.getColor(getActivity(), android.R.color.white));
menuIcon4.setColorFilter(ContextCompat.getColor(getActivity(), android.R.color.white));
menuIcon1.setPadding(15,15,15,15);
menuIcon2.setPadding(15,15,15,15);
menuIcon3.setPadding(15,15,15,15); //设置的是各个控件之间的距离menuIcon4.setPadding(15,15,15,15);
SubActionButton.BuilderitemBuilder = new SubActionButton.Builder(getActivity()); itemBuilder.setLayoutParams(new youtParams(160,160));
ImageViewmenuInfo = (ImageView) view.findViewById(_menu);
actionMenu= new FloatingActionMenu.Builder(getActivity())
.addSubActionView(itemBuilder.setContentView(menuIcon1).build()) .addSubActionView(itemBuilder.setContentView(menuIcon2).build()) .addSubActionView(itemBuilder.setContentView(menuIcon3).build()) .addSubActionView(itemBuilder.setContentView(menuIcon4).build()) .attachTo(menuInfo)
.setStartAngle(100)
.setEndAngle(260)
.build();
menuIcon1.setId(R.id.itemId1);
menuIcon1.setOnClickListener(this);
menuIcon2.setId(R.id.itemId2);
menuIcon2.setOnClickListener(this);
menuIcon3.setId(R.id.itemId3);
menuIcon3.setOnClickListener(this);
menuIcon4.setId(R.id.itemId4);
menuIcon4.setOnClickListener(this);
}
private void toSmsManager() {
name = (TextView) getActivity().findViewById(_name);
phone = (TextView) getActivity().findViewById(_phone);
Intent intent = new Intent(getActivity(), ContactSmsActivity.class);
intent.putExtra("name", name.getText().toString());
intent.putExtra("phone", phone.getText().toString());
startActivity(intent);
}
private void callContact() {
phone = (TextView) getActivity().findViewById(_phone);
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phone.getText().toString()));
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(callIntent);
} else {
Toast toast = Toast.makeText(getActivity(), R.string.alert_no_call_perm, Toast.LENGTH_LONG);
toast.show();
}
}
private void editContact() {
name = (TextView) getActivity().findViewById(_name);
Contact contact = db.getContactByName(name.getText().toString());
Intent intent = new Intent(getActivity(), ContactEditionActivity.class);
intent.putExtra("contact", contact);
startActivity(intent);
}
private void deleteContact() {
AlertDialog dialog = new AlertDialog.Builder(getActivity())
.setMessage(getResources().getString(R.string.alert_delete_message))
.setCancelable(false)
.setPositiveButton(getResources().getString(R.string.alert_delete_ok),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
TextViewtextView = (TextView) getActivity().findViewById(_name);
Contact contact =
db.getContactByName(textView.getText().toString());
db.deleteContact(contact);
List<Sms>allSms = db.getAllSmsFromContact(contact.getId());
for (Smssms : allSms) {
db.deleteSms(sms);
}
Intent intent = new Intent(getActivity(), MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
})
.setNegativeButton(getResources().getString(R.string.alert_delete_cancel), null)
.show();
Button buttonPositive = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
Button buttonNegative = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
buttonPositive.setTextColor(ContextCompat.getColor(getActivity(),
R.color.colorPrimary));
buttonNegative.setTextColor(ContextCompat.getColor(getActivity(),
R.color.colorPrimary));
}
}
Main程序:
package com.example.hivian.my_contact_manager.views.activities;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import com.example.hivian.my_contact_manager.R;
import com.example.hivian.my_contact_manager.models.Contact;
import com.example.hivian.my_contact_manager.utilities.Utility;
import com.example.hivian.my_contact_manager.views.fragments.ContactInfoFragment;
import com.example.hivian.my_contact_manager.views.fragments.ContactListFragment;
public class MainActivity extends AppCompatActivity implements
ContactListFragment.DataPassListener {
private ContactListFragment fragmentA;
private ContactInfoFragment fragmentB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(yout.main_activity);
if (findViewById(R.id.fragment_holder) != null) {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);
if (savedInstanceState != null) {
fragmentA= new ContactListFragment();
fragmentB= new ContactInfoFragment();
return;
}
fragmentA= new ContactListFragment();
fragmentB= new ContactInfoFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_holder, fragmentA)
.commit();
Utility.changeStatusBarColor(this);
}
}
@Override
public void onBackPressed() {
int count = getSupportFragmentManager().getBackStackEntryCount();
if (count == 0) {
super.onBackPressed();
} else {
if (fragmentB.actionMenu!= null) {
fragmentB.actionMenu.close(true);
}
getSupportFragmentManager().popBackStack();
}
}
@Override
public void passData(Contact contact) {
if (fragmentB!= null) {
Bundle args = new Bundle();
args.putSerializable("contact", contact);
fragmentB.setArguments(args);
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right)
.replace(R.id.fragment_holder, fragmentB)
.addToBackStack(null)
五、实验数据(现象)处理分析
下面是程序的截图:
软件界面以及各个自文件目录示意图:
通讯录在Android手机功能界面上的显示图标以及图标在工程里存放的位置:
进入通讯录时的初界面以及图标的配置过程:
刚开始运行通讯录时,出现几个权限选择:
开始界面(按加号):创建一个新的联系人:会弹出键盘
创建联系人的过程中如果格式不规范会有错误提示:比如Email格式错误创建好后的界面(多创建几个联系人),头像可以更改:
修改头像时出现的错误如下(照相机和相册里选择合适的头像):
接下来进行对通讯录的基本功能测试。
基本功能有对已保存联系人信息的编辑(更新),删除,发短信和打电。
下面是功能显示图:
编辑已存联系人信息(添加Email):
编辑的时候还有对Text部分的选择复制,粘贴功能:
删除联系人操作短信功能打电话功能
第而个联系人(llp)已经被删除。
六、实验结论。