好友
阅读权限10
听众
最后登录1970-1-1
|
拨打电话app的设计与实现说明文档
一、软件名称
拨打电话与发短信app
二、软件内容简介
【说明】:
模仿小米手机的拨打电话功能,由于电脑的问题,无法授予androidstudio安装app在手机,只能安装在手机模拟器中,所以无法实现真正的拨打电话和发送短信,只能用提示信息模拟
【功能】:
1、主界面只显示联系人的名字,点进去才能显示全部信息,并可以选择拨打电话或是发送短信功能,页面简洁,迎合正常人的使用习惯
2、返回键退出程序时,不直接退出,先提醒用户是否退出
3、通信录的右下角有一个“+”的按钮,点击便可以添加新联系人
4、长按主界面的联系人便可删除
【知识归纳】:
1、用intent实现activity的跳转
2、用listview实现批量信息的展示
3、用sqlite数据库存储联系人信息,并实现增删功能
4、用ContextMenu实现长按删除listview某个项的功能
5、用onKeyDown提示用户是否退出程序
三、代码实现
(一)JAVA代码
(1)AddUserActivity
package cn.itcast.phoneclient3;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AddUserActivity extends AppCompatActivity implements View.OnClickListener{
private MyDatabaseHelper dbHelper;
private EditText username,telnumber;
private Button ok_add,create_database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_user);
dbHelper = new MyDatabaseHelper(this,"User2.db",null,1);
username = (EditText) findViewById(R.id.add_user_name);
telnumber = (EditText) findViewById(R.id.add_user_telnumber);
ok_add = (Button) findViewById(R.id.ok_add);
ok_add.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.ok_add:{
String name = username.getText().toString();
String tel_number = telnumber.getText().toString();
if(name == null || tel_number == null){
Toast.makeText(this,"用户名和电话不能为空!",Toast.LENGTH_SHORT).show();
}else{
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name",name);
values.put("telnumber",tel_number);
db.insert("User2",null,values);
values.clear();
Toast.makeText(this,"添加成功!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(AddUserActivity.this,MainActivity.class);
startActivity(intent);
this.finish();
db.close();
}
}
}
}
}
(2)MainActivity
package cn.itcast.phoneclient3;
import android.bluetooth.BluetoothManager;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private MyDatabaseHelper dbHelper;
private List<User> UserList = new ArrayList<>();
private ImageButton addUser;
UserAdapter adapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addUser = (ImageButton) findViewById(R.id.addUser);
addUser.setOnClickListener(this);
dbHelper = new MyDatabaseHelper(this,"User2.db",null,1);
//初始化数据
initUser();
adapter = new UserAdapter(MainActivity.this,
R.layout.user_item,UserList);
listView = (ListView) findViewById(R.id.user_list);
//为 ListView 的所有 item 注册 ContextMenu
this.registerForContextMenu(listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent,View view,
int position,long id){
User user = UserList.get(position);
String name = user.getName();
String telnumber = user.getTelnumber();
Intent intent = new Intent(MainActivity.this,UserActivity.class);
intent.putExtra("name",name);
intent.putExtra("telnumber",telnumber);
startActivity(intent);
}
});
}
private void initUser(){
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("User2",null,null,null,null,null,null);
if(cursor.moveToFirst()){
do{
//遍历
String name = cursor.getString(cursor.getColumnIndex("name"));
String telnumber = cursor.getString(cursor.getColumnIndex("telnumber"));
User user = new User(name,telnumber,R.mipmap.ic_launcher);
UserList.add(user);
}while(cursor.moveToNext());
}
cursor.close();
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.addUser:{
Intent intent = new Intent(MainActivity.this,AddUserActivity.class);
startActivity(intent);
this.finish();
}
break;
}
}
//实现长按删除功能
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0)
{
new AlertDialog.Builder(this).setTitle("系统提示").setMessage("确定退出通信录吗").setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
android.os.Process.killProcess(android.os.Process.myPid());
}
}).setNegativeButton("取消", null).show();
return true;
}
else if(keyCode== KeyEvent.KEYCODE_MENU && event.getRepeatCount()==0)
{
openOptionsMenu();
return true;
}
return true;
}
public void onCreateContextMenu(ContextMenu menu,View view,ContextMenu.ContextMenuInfo menuInof)
{
super.onCreateContextMenu(menu, view, menuInof);
menu.add(0,1,Menu.NONE,"删除");
}
public boolean onContextItemSelected(MenuItem item)
{
AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId())
{
case 1:
//删除列表项...
int pos=(int)listView.getAdapter().getItemId(menuInfo.position);
if(UserList.remove(pos)!=null){//这行代码必须有
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
String st=Integer.toString(pos+1);
String s="DELETE FROM User2 WHERE ID = "+st;
db.execSQL(s);
db.close();
Toast.makeText(this,"删除成功!", Toast.LENGTH_SHORT).show();
}else {
System.out.println("failed");
}
adapter.notifyDataSetChanged();//刷新view
break;
default:
return super.onContextItemSelected(item);
}
return true;
}
}
(3)MessageActivity
package cn.itcast.phoneclient3;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MessageActivity extends AppCompatActivity {
private Button bt;
private TextView et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
bt=(Button)findViewById(R.id.button);
et=(TextView) findViewById(R.id.ev1);
Intent intent = getIntent();
String telephone = intent.getStringExtra("telnumber");
et.setText(telephone);
}
public void click(View view){
Toast.makeText(this,"发送成功...",Toast.LENGTH_SHORT).show();
}
}
(4)MyDataBaseHelper.java
package cn.itcast.phoneclient3;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
/**
* Created by xiaomo on 2019/11/11.
*/
public class MyDatabaseHelper extends SQLiteOpenHelper {
public static final String Create_User_Table = "create table User2 ("
+ "name text,"
+ "telnumber text)";
private Context mContext;
public MyDatabaseHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,
int version){
super(context,name,factory,version);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(Create_User_Table);
Toast.makeText(mContext,"创建成功",Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
(5)phoneActivity
pack package cn.itcast.phoneclient3;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class phoneActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone);
}
}
(6)User.java
package cn.itcast.phoneclient3;
/**
* Created by xiaomo on 2019/11/11.
*/
public class User {
private int imgId;
private String name;
private String telnumber;
public User(String name,String telnumber,int imgId){
this.name = name;
this.telnumber = telnumber;
this.imgId = imgId;
}
public int getImgId(){
return imgId;
}
public String getName(){
return name;
}
public String getTelnumber(){
return telnumber;
}
}
(7)UserActivity
package cn.itcast.phoneclient3;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class UserActivity extends AppCompatActivity implements View.OnClickListener{
private TextView name,telnumber;
private Button bt1,bt2;
String user_name;
String telephone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
name = (TextView) findViewById(R.id.text_name);
telnumber = (TextView) findViewById(R.id.text_number);
bt1=(Button)findViewById(R.id.button1);
bt2=(Button)findViewById(R.id.button2);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
Intent intent = getIntent();
user_name = intent.getStringExtra("name");
name.setText(user_name);
telephone = intent.getStringExtra("telnumber");
telnumber.setText(telephone);
}
public void onClick(View v){
switch(v.getId()) {
case R.id.button1:
Intent intent1=new Intent(UserActivity.this,phoneActivity.class);
startActivity(intent1);
break;
case R.id.button2:
Intent intent2=new Intent(UserActivity.this,MessageActivity.class);
intent2.putExtra("name",user_name);
intent2.putExtra("telnumber",telephone);
startActivity(intent2);
break;
}
}
}
(8)UserAdapter.java
package cn.itcast.phoneclient3;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by xiaomo on 2019/11/11.
*/
public class UserAdapter extends ArrayAdapter<User> {
private int resourceId;
public UserAdapter(Context context, int textViewResourceId, List<User> objects){
super(context,textViewResourceId,objects);
resourceId = textViewResourceId;
}
@Override
public View getView(int position,View converView,ViewGroup parent){
User user = getItem(position);//获取当前User实例
View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
ImageView userimg = (ImageView) view.findViewById(R.id.user_img);
TextView username = (TextView) view.findViewById(R.id.user_name);
userimg.setImageResource(user.getImgId());
username.setText(user.getName());
return view;
}
}
(二)界面布局
(1)activity_add_user.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cn.itcast.phoneclient3.AddUserActivity">
<EditText
android:id="@+id/add_user_name"
android:hint="姓名"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/add_user_telnumber"
android:hint="电话号码"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/add_user_name"/>
<Button
android:id="@+id/ok_add"
android:text="添加"
android:textSize="18dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/add_user_telnumber"/>
</RelativeLayout>
(2)activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cn.itcast.phoneclient3.MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="联系人"
android:layout_centerHorizontal="true"
android:textSize="30dp"
/>
<ListView
android:id="@+id/user_list"
android:layout_below="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
<ImageButton
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/addUser"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@drawable/add"
/>
</RelativeLayout>
(3)activity_message.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="cn.itcast.phoneclient3.MessageActivity">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发给"
android:textSize="30dp"
/>
<TextView
android:id="@+id/ev1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv1"
/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ev1"
android:text="内容"
android:textSize="30dp"
/>
<EditText
android:id="@+id/ev2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="click"
/>
</RelativeLayout>
(4)activity_phone.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="cn.itcast.phoneclient3.phoneActivity">
<ImageView
android:id="@+id/imv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/phone"
/>
</RelativeLayout>
(5)activity_user.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cn.itcast.phoneclient3.UserActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="联系人详细情况"
android:layout_centerHorizontal="true"
android:textSize="30dp"
android:id="@+id/textView3" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="92dp"
android:text="姓名"
android:textSize="20dp"
android:layout_marginLeft="70dp"
android:layout_marginStart="39dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_below="@+id/textView"
android:layout_marginTop="29dp"
android:text="电话"
android:textSize="20dp"/>
<TextView
android:id="@+id/text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView"
android:layout_marginLeft="49dp"
android:layout_marginStart="49dp"
android:layout_toEndOf="@+id/textView"
android:layout_toRightOf="@+id/textView"
android:textSize="20dp"/>
<TextView
android:id="@+id/text_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_alignStart="@+id/text_name"
android:layout_alignLeft="@+id/text_name"
android:layout_alignRight="@+id/text_name"
android:textSize="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打电话"
android:id="@+id/button1"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/textView3"
android:layout_alignStart="@+id/textView3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发短信"
android:id="@+id/button2"
android:layout_alignTop="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:layout_toEndOf="@+id/button1"
android:layout_marginLeft="44dp"
android:layout_marginStart="44dp" />
</RelativeLayout>
(6)user_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/user_img"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/user_name"
android:textSize="18dp"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/user_img"/>
</RelativeLayout>
|
|