最近写数据库,我就自己写了个带密码的个人通讯录,感觉sqlite特别好用,和mysql,sqlserver都一样,真是简单的关系型数据库,注意:开启数据库,cursor后一定要记得关闭close()掉,避免浪费资源。另外adapter的notifyDataSetChanged()这个方法也特别好用,就是数据库更新的时候,调用一下,baseAdapter中的getview就会重新加载一遍,这样界面就会更新数据,而不用onCreate()方法来更新,另外可以把notifyDataSetChanged()方法写在onResume()方法中,这样在两个activity跳转后按back 键也可以达到刷新界面的效果!
想要源码的可以留言,有问题可以留言,同时也欢迎指正我的纰漏;
转载请标明出处:http://blog.csdn.net/wdaming1986/article/details/6727032
另:csdn下载连接地址:http://download.csdn.net/source/3555843
程序启动后输入密码的界面: 第一次启动程序和点击修改密码界面:
点击确定,进入联系人列表界面: 点击menu菜单,有两个菜单键:
单击每一个列表进入修改界面: 长按每一个联系人弹dialog删除记录:
下面看代码:
在com.cn.daming包下的类:
1、Login.java 程序的入口类
package com.cn.daming; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.cn.daming.databases.DBOpenHelper; public class Login extends Activity { private DBOpenHelper db; private EditText et; private Button login_button; private String password; @Override protected void onCreate(Bundle bundle) { super.onCreate(bundle); db = new DBOpenHelper(this); password = db.getPwd(); db.close(); if(password.equals("")){ Intent intent = new Intent(Login.this,PasswordManage.class); startActivity(intent); finish(); return; } setContentView(R.layout.login); et = (EditText)findViewById(R.id.login_password); login_button = (Button)findViewById(R.id.note_login); login_button.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { String input_pwd = et.getText().toString(); if(password.equals(input_pwd)){ Toast.makeText(Login.this, R.string.login_success, Toast.LENGTH_LONG).show(); Intent intent = new Intent(Login.this,MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); finish(); }else{ Toast.makeText(Login.this, R.string.error_password, Toast.LENGTH_LONG).show(); et.setText(""); } } }); } }
2、MainActivity.java类:程序listView列表显示类
package com.cn.daming; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.ListView; import android.widget.Toast; import com.cn.daming.adapter.ContantsAdapter; import com.cn.daming.databases.DBOpenHelper; public class MainActivity extends Activity { private DBOpenHelper dbOpenHelper; private ContantsAdapter contantsAdapter; private ListView dbListView; private Cursor cursor; final int MENU_ADD = Menu.FIRST; final int MENU_CHANGE = Menu.FIRST+1; private List<String> ids; private List<String> names; private List<String> phones; AlertDialog dialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); dbOpenHelper = new DBOpenHelper(this); dbListView = (ListView)findViewById(R.id.db_listview); refreshDBOpenHelper(); dbListView.setOnItemClickListener(new OnItemClickListener(){ public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { Intent intent= new Intent(); intent.putExtra("cmd", 0); String contants_id = ids.get(position);//0代表查询联系人,1代表添加联系人 intent.putExtra("id", contants_id); intent.setClass(MainActivity.this, DetailContantsActivity.class); startActivity(intent); } }); dbListView.setOnItemLongClickListener(new OnItemLongClickListener(){ public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int position, long arg3) { dialog = new AlertDialog.Builder(MainActivity.this) .setTitle("提示!!") .setMessage("确定要删除这条记录?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String contants_id = ids.get(position); dbOpenHelper.deleteContants(contants_id); dbOpenHelper.close(); Toast.makeText(MainActivity.this, "正在删除数据库,请稍后。。。", Toast.LENGTH_LONG).show(); refreshDBOpenHelper(); contantsAdapter.notifyDataSetChanged(); } }) .setNegativeButton("取消",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.dismiss(); } }).show(); return false; } }); } @Override protected void onResume() { refreshDBOpenHelper(); contantsAdapter.notifyDataSetChanged(); super.onResume(); } public void refreshDBOpenHelper(){ cursor = dbOpenHelper.selectContants(); ids = new ArrayList<String>(); names = new ArrayList<String>(); phones = new ArrayList<String>(); int count = cursor.getCount(); if(count>0){ for(int i=0;i<count;i++){ cursor.moveToPosition(i); ids.add(cursor.getString(0)); names.add(cursor.getString(1)); phones.add(cursor.getString(2)); } }else{ Toast.makeText(this, R.string.not_dbcursor_values, Toast.LENGTH_SHORT).show(); } contantsAdapter = new ContantsAdapter(this,names,phones); dbListView.setAdapter(contantsAdapter); cursor.close(); dbOpenHelper.close(); } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, MENU_ADD, 0, R.string.menu_add) .setIcon(R.drawable.add); //add - add_button menu.add(0, MENU_CHANGE, 0, R.string.menu_change) .setIcon(R.drawable.modify); //add -add_change_password return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()){ // case MENU_ADD: //press change add button Intent add_intent= new Intent(this,DetailContantsActivity.class); add_intent.putExtra("cmd", 1); startActivity(add_intent); break; case MENU_CHANGE: //press change password button Intent change_password_intent = new Intent(MainActivity.this,PasswordManage.class); startActivityForResult(change_password_intent,0); break; } return super.onOptionsItemSelected(item); } }
3、PasswordManage.java,密码设置类:
package com.cn.daming; import com.cn.daming.databases.DBOpenHelper; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class PasswordManage extends Activity { private EditText et1; private EditText et2; private Button button; private DBOpenHelper db; private int CHANGE_PWD_SUCCESS = 100; public void onCreate(Bundle bundle){ super.onCreate(bundle); db = new DBOpenHelper(this); setContentView(R.layout.password_manage); et1 = (EditText)findViewById(R.id.new_password); et2 = (EditText)findViewById(R.id.repeat_password); button = (Button)findViewById(R.id.save_password); button.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { String new_pwd = et1.getText().toString(); String repeat_pwd = et2.getText().toString(); if(new_pwd==null||new_pwd.equals("")){ Toast.makeText(PasswordManage.this, R.string.password_is_null, Toast.LENGTH_LONG).show(); }else{ if(new_pwd.equals(repeat_pwd)){ if(db.getPwd().equals("")){ db.insertPwd(new_pwd); Toast.makeText(PasswordManage.this, R.string.password_set_success, Toast.LENGTH_LONG).show(); toNoteList(); }else{ db.updatePwd(new_pwd); Toast.makeText(PasswordManage.this, R.string.password_change_success, Toast.LENGTH_LONG).show(); PasswordManage.this.setResult(CHANGE_PWD_SUCCESS); PasswordManage.this.finish(); } }else{ Toast.makeText(PasswordManage.this, R.string.password_isnot_equal, Toast.LENGTH_LONG).show(); } } } }); } public void toNoteList(){ Toast.makeText(PasswordManage.this, R.string.password_set_success, Toast.LENGTH_LONG); Intent intent = new Intent(PasswordManage.this,MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); finish(); } }
4、DetailContantsActivity。java类,每一条记录,联系人的类:
package com.cn.daming; import android.app.Activity; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.ImageButton; import android.widget.Toast; import com.cn.daming.databases.DBOpenHelper; public class DetailContantsActivity extends Activity{ Cursor cursor; DBOpenHelper dbOpenHelper; int id = -1; int [] textIds ={ R.id.etName, R.id.etPhone, R.id.etMobile, R.id.etEmail, R.id.etPost, R.id.etAddr, R.id.etComp }; EditText [] textArray; ImageButton saveButton; //save Button int status = -1; //0表示查看信息,1表示添加联系人,2表示修改联系人 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.contants_detail); textArray = new EditText[textIds.length]; for(int i=0;i<textIds.length;i++){ textArray[i] = (EditText)findViewById(textIds[i]); } initSaveImageButton(); dbOpenHelper = new DBOpenHelper(this); Intent intent = getIntent(); status = intent.getExtras().getInt("cmd"); switch(status){ case 0: String contants_id = intent.getExtras().getString("id"); cursor = dbOpenHelper.getContants(contants_id); int count = cursor.getCount(); if(count == 0){ Toast.makeText(this, "对不起,没有找到指定的联系人!", Toast.LENGTH_LONG).show(); } else{ cursor.moveToFirst(); textArray[0].setText(cursor.getString(1)); //设置姓名框中的内容 textArray[1].setText(cursor.getString(2)); //设置固话框中的内容 textArray[2].setText(cursor.getString(3)); //设置手机号码框中的内容 textArray[3].setText(cursor.getString(4)); //设置电子邮件框中的内容 textArray[4].setText(cursor.getString(5)); //设置电子邮件框中的内容 textArray[5].setText(cursor.getString(6)); //设置电子邮件框中的内容 textArray[6].setText(cursor.getString(7)); //设置电子邮件框中的内容 } cursor.close(); dbOpenHelper.close(); break; case 1: //新建详细人信息 for(EditText et:textArray){ et.getEditableText().clear(); //清空各个EditText控件中内容 } break; } } public void initSaveImageButton() { saveButton = (ImageButton)findViewById(R.id.detailSave); saveButton.setOnClickListener(new OnClickListener(){ public void onClick(View arg0) { String [] strArray = new String[textArray.length]; for(int i=0;i<strArray.length;i++){ strArray[i] = textArray[i].getText().toString().trim(); //获得用户输入的信息数组 } if(strArray[0].equals("") || strArray[1].equals("")){ Toast.makeText(DetailContantsActivity.this, "对不起,请将姓名和电话填写完整!", Toast.LENGTH_LONG).show(); }else{ switch(status){ //判断当前的状态 case 0: //查询联系人详细信息时按下保存 updateContact(strArray); //更新联系人信息 break; case 1: //新建联系人时按下保存按钮 insertContact(strArray); //插入联系人信息 break; } } } }); } public void insertContact(String [] strArray){ long count = dbOpenHelper.insertContants(strArray); //插入数据 dbOpenHelper.close(); if(count == -1){ Toast.makeText(this, "添加联系人失败!", Toast.LENGTH_LONG).show(); } else{ Toast.makeText(this, "添加联系人成功!", Toast.LENGTH_LONG).show(); } } public void updateContact(String [] strArray){ int count = dbOpenHelper.updateContants(id+"", strArray); //更新数据库 dbOpenHelper.close(); if(count == 1){ Toast.makeText(this, "修改联系人成功!", Toast.LENGTH_LONG).show(); } else{ Toast.makeText(this, "修改联系人失败!", Toast.LENGTH_LONG).show(); } } }
在com.cn.daming.databases包下面的类:
5、DBOpenHelper。java,数据库的类:
package com.cn.daming.databases; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBOpenHelper extends SQLiteOpenHelper{ private final static String DATABASE_NAME = "personal_contacts"; private final static int DATABASE_VERSION = 1; private final static String TABLE_NAME = "contants"; public static final String ID="_id"; //ID public static final String NAME="name"; //名称 public static final String PHONE="phone"; //固定电话 public static final String MOBILE="mobile";//手机号码 public static final String EMAIL="email"; //电子邮件地址 public static final String POST="post"; //邮政编码 public static final String ADDR="addr"; //通信地址 public static final String COMP="comp"; //公司 public final static String LOGIN_TABLE_NAME = "contantslogin"; public final static String LOGIN_USER = "admin"; public final static String LOGIN_PWD = "password"; public DBOpenHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String sql = "create table "+TABLE_NAME+" (" + ID + " integer primary key autoincrement," + NAME + " varchar," + PHONE+" varchar," + MOBILE + " varchar," + EMAIL + " varchar," + POST + " varchar," + ADDR + " varchar," + COMP + " varchar)"; db.execSQL(sql); sql = "create table "+LOGIN_TABLE_NAME+" (" +LOGIN_USER+" text, " +LOGIN_PWD+" text )"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { String sql = "drop table if exists "+TABLE_NAME; db.execSQL(sql); sql = "drop table if exists "+LOGIN_TABLE_NAME; db.execSQL(sql); onCreate(db); } public Cursor selectContants(){ SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null); return cursor; } public long insertContants(String[] strArray){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put(NAME, strArray[0]); cv.put(PHONE, strArray[1]); cv.put(MOBILE, strArray[2]); cv.put(EMAIL, strArray[3]); cv.put(POST, strArray[4]); cv.put(ADDR, strArray[5]); cv.put(COMP, strArray[6]); return db.insert(TABLE_NAME, null, cv); } public void deleteContants(String id){ SQLiteDatabase db = this.getWritableDatabase(); String where = ID+"=?"; String[] whereValues = {id}; db.delete(TABLE_NAME, where, whereValues); } public int updateContants(String id,String[] strArray){ SQLiteDatabase db = this.getWritableDatabase(); String where = ID+"=?"; String[] whereValues = {id}; ContentValues cv = new ContentValues(); cv.put(NAME, strArray[0]); cv.put(PHONE, strArray[1]); cv.put(MOBILE, strArray[2]); cv.put(EMAIL, strArray[3]); cv.put(POST, strArray[4]); cv.put(ADDR, strArray[5]); cv.put(COMP, strArray[6]); return db.update(TABLE_NAME, cv, where, whereValues); } public Cursor getContants(String id){ SQLiteDatabase db = this.getReadableDatabase(); String where = ID+"=?"; String[] whereValues = {id}; Cursor cursor = db.query(TABLE_NAME, null, where, whereValues, null, null, null); return cursor; } public long insertPwd(String password){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put(LOGIN_USER, LOGIN_USER); cv.put(LOGIN_PWD, password); return db.insert(LOGIN_TABLE_NAME, null, cv); } public int updatePwd(String password){ SQLiteDatabase db = this.getWritableDatabase(); String where = LOGIN_USER+"=?"; String[] whereValues = {LOGIN_USER}; ContentValues cv = new ContentValues(); cv.put(LOGIN_PWD, password); return db.update(LOGIN_TABLE_NAME, cv, where, whereValues); } public String getPwd(){ SQLiteDatabase db = this.getReadableDatabase(); String where = LOGIN_USER+"=?"; String[] whereValues = {LOGIN_USER}; Cursor cursor = db.query(LOGIN_TABLE_NAME, null, where, whereValues, null, null, null); if(cursor.moveToFirst()){ return cursor.getString(cursor.getColumnIndex(LOGIN_PWD)); }else{ return ""; } } }
在com.cn.daming.adapter包下的类:
6、ContantsAdapter。java,适配器类:
package com.cn.daming.adapter; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.cn.daming.R; public class ContantsAdapter extends BaseAdapter { private List<String> names; private List<String> phones; private LayoutInflater inflater; private Context context; public ContantsAdapter(Context context,List<String> names ,List<String> phones){ inflater = LayoutInflater.from(context); this.names = names; this.phones = phones; this.context = context; } public int getCount() { return names.size(); } public Object getItem(int position) { return names.get(position); } public long getItemId(int position) { return position; } public View getView(int position, View view, ViewGroup group) { ContantsHolder holder = new ContantsHolder(); if(view==null){ view = inflater.inflate(R.layout.contants_list_view, null); holder.contansName = (TextView)view.findViewById(R.id.name_textview); holder.contantsPhone = (TextView)view.findViewById(R.id.phone_textview); view.setTag(holder); }else{ holder = (ContantsHolder)view.getTag(); } holder.contansName.setText(names.get(position)); holder.contantsPhone.setText(phones.get(position)); return view; } public class ContantsHolder { private TextView contansName; private TextView contantsPhone; } }
布局文件
1、login。xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingTop="10dip" android:orientation="vertical" android:background="@drawable/background" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/please_input_password" android:textSize="24dip" android:textStyle="bold" android:textColor="@drawable/black" android:layout_gravity="center" /> <EditText android:id="@+id/login_password" android:layout_width="250dip" android:layout_height="wrap_content" android:password="true" android:layout_gravity="center" android:layout_marginTop="15dip" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="15dip" > <Button android:id="@+id/note_login" android:layout_width="100dip" android:layout_height="wrap_content" android:text="@string/ok" /> </LinearLayout> </LinearLayout>
2、contants_detail.xml布局文件
<?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="wrap_content" android:background="@drawable/background" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" > <!-- 显示联系人姓名线性布局 --> <TextView android:layout_width="100px" android:layout_height="wrap_content" android:textSize="18px" android:textColor="@color/text" android:layout_gravity="left|center_vertical" android:text="@string/contantsName" /> <EditText android:id="@+id/etName" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 显示联系人固定电话的线性布局 --> <TextView android:layout_width="100px" android:layout_height="wrap_content" android:textSize="18px" android:textColor="@color/text" android:layout_gravity="center_vertical|left" android:text="@string/contantsPhone" /> <EditText android:id="@+id/etPhone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:phoneNumber="true" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 显示联系人手机号码的线性布局 --> <TextView android:layout_width="100px" android:layout_height="wrap_content" android:textSize="18px" android:textColor="@color/text" android:layout_gravity="left|center_vertical" android:text="@string/contantsMobile" /> <EditText android:id="@+id/etMobile" android:layout_width="fill_parent" android:layout_height="wrap_content" android:phoneNumber="true" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 显示联系人电子邮件的线性布局 --> <TextView android:layout_width="100px" android:layout_height="wrap_content" android:textSize="18px" android:textColor="@color/text" android:layout_gravity="left|center_vertical" android:text="@string/contantsEmail" /> <EditText android:id="@+id/etEmail" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 显示联系人邮编的线性布局 --> <TextView android:layout_width="100px" android:layout_height="wrap_content" android:textSize="18px" android:textColor="@color/text" android:layout_gravity="left|center_vertical" android:text="@string/contantsPost" /> <EditText android:id="@+id/etPost" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 显示联系人通信地址的线性布局 --> <TextView android:layout_width="100px" android:layout_height="wrap_content" android:textSize="18px" android:textColor="@color/text" android:layout_gravity="left|center_vertical" android:text="@string/contantsAddr" /> <EditText android:id="@+id/etAddr" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 显示联系人公司的线性布局 --> <TextView android:layout_width="100px" android:layout_height="wrap_content" android:textSize="18px" android:textColor="@color/text" android:layout_gravity="left|center_vertical" android:text="@string/contantsComp" /> <EditText android:id="@+id/etComp" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <ImageButton android:id="@+id/detailSave" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/save" /> </LinearLayout>
3、contants_list_view.xml,每一个list的item布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:id="@+id/name_textview" android:layout_width="150dip" android:layout_height="wrap_content" android:layout_marginRight="10dip" android:textColor="#000000" android:textSize="10pt" /> <TextView android:id="@+id/phone_textview" android:layout_width="165dip" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="10pt" /> </LinearLayout>
4、main.xml布局文件
<?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" android:background="@drawable/background" > <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/title" android:textSize="24px" android:textColor="@color/text" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/title" /> </LinearLayout> <ListView android:id="@+id/db_listview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:choiceMode="singleChoice" /> </LinearLayout>
5、password_manage.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="@drawable/background" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textStyle="bold" android:textSize="26dip" android:padding="6dip" android:paddingBottom="10dip" android:textColor="@drawable/black" android:text="@string/password_manage" /> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dip" android:layout_marginLeft="10dip" > <TableRow android:layout_width="fill_parent" android:layout_height="50dip" > <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="@string/new_password" android:layout_gravity="right" android:gravity="center" android:textColor="#000000" android:textSize="11pt" /> <EditText android:id="@+id/new_password" android:layout_width="180dip" android:layout_height="wrap_content" android:password="true" android:layout_marginLeft="20dip" /> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="50dip" android:layout_marginTop="10dip" android:gravity="center_vertical" > <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="@string/repeat_password" android:layout_gravity="right" android:gravity="center_vertical" android:textColor="#000000" android:textSize="11pt" /> <EditText android:id="@+id/repeat_password" android:layout_width="180dip" android:layout_height="wrap_content" android:password="true" android:layout_marginLeft="20dip" /> </TableRow> </TableLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_marginTop="20dip" > <Button android:id="@+id/save_password" android:layout_width="80dip" android:layout_height="wrap_content" android:text="@string/ok" android:textSize="16dip" /> </LinearLayout> </LinearLayout>
values下的文件
1、string。xml文件
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">ContantsApp</string> <string name="please_input_password">请输入大明通讯录密码:</string> <string name="ok">确定</string> <string name="login_success">恭喜,登陆成功!</string> <string name="error_password">您输入的密码错误,请重新输入!</string> <string name="password_manage">大明通讯录密码管理:</string> <string name="new_password">新密码:</string> <string name="repeat_password">再一次:</string> <string name="password_is_null">输入密码为空,请输入密码!</string> <string name="password_set_success">密码设置成功!</string> <string name="password_change_success">密码修改成功!</string> <string name="password_isnot_equal">两次输入的密码不一致,请重新输入密码!</string> <string name="title">大明通讯录列表</string> <string name="not_dbcursor_values">数据库中没有记录,请点击菜单新建通讯录!</string> <string name="contantsName">姓名:</string> <string name="contantsPhone">固定电话:</string> <string name="contantsMobile">移动电话:</string> <string name="contantsEmail">电子邮件:</string> <string name="contantsPost">邮政编码:</string> <string name="contantsAddr">通讯地址:</string> <string name="contantsComp">公司地址:</string> <string name="menu_add">增加</string> <string name="menu_change">修改密码</string> </resources>
2、color。xml文件
<?xml version="1.0" encoding="utf-8"?> <resources> <drawable name="white">#FFFFFF</drawable> <drawable name="black">#FF000000</drawable> <drawable name="dackgray">#666666</drawable> <drawable name="red">#FFFF0000</drawable> <drawable name="blue">#FF0000FF</drawable> <drawable name="yellow">#FFFFFF00</drawable> <drawable name="green">#FF00FF00</drawable> <color name="text">#000000</color> <drawable name="changshise">#FFD19275</drawable> </resources>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cn.daming" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Login" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity"></activity> <activity android:name=".PasswordManage"></activity> <activity android:name=".DetailContantsActivity"></activity> </application> </manifest>