前言
使用Android应用时,系统通过弹出一些询问或者选择的对话框和用户进行交互,进而完成操作。这里,使用AlterDialog进行对话框设置。一般我们实例化AlterDialog中Builder类,然后使用以下方法进行设置界面:
setTitle() : 为对话框设置标题
setIcon() : 为对话框设置图标
setMessgae() : 为对话框设置内容
setView() : 为对话框设置自定义样式
setItems() : 设置对话框要显示的一个list,一般用于在显示几个命令时
setMultiChoiceItems() : 用来设置对话框显示的一系列复选框
setNeutraButton() : 普通按钮
setPositiveButton() : 给对话框添加“YES”按钮
setNegativeButton() : 给对话框添加“NO”按钮
create() : 创建对话框
show() : 显示对话框
Android代码:
package cn.zhuangzhihuang.mydialog;
import java.util.ArrayList;
import java.util.List;
import cn.zhuangzhihuang.mydialog.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
List<Person> friend_List;
private MyAdapter myAdapter;
private int this_ID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView data_view = (ListView) findViewById(R.id.data_view);
Person[] DB = {
new Person("张三","18555555555"),
new Person("李四","18555555556"),
new Person("王五","18555555557"),
new Person("赵六","18555555558"),
new Person("邓七","18555555559")
};
friend_List = new ArrayList<Person>();
for(int i=0;i<DB.length;i++) {
friend_List.add(DB[i]);
}
//自定义适配器
myAdapter = new MyAdapter();
data_view.setAdapter(myAdapter);
registerForContextMenu(data_view);
// data_view.setOnItemClickListener(new OnItemClickListener() {
//
// @Override
// public void onItemClick(AdapterView<?> parent, View view,
// int position, long id) {
// // TODO Auto-generated method stub
// String temp = friend_List.get((int)id).toString();
// Toast.makeText(MainActivity.this, temp, 0).show();
//
// }
// }); //显示事件监听
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() { //返回表的长度
// TODO Auto-generated method stub
return friend_List.size();
}
@Override
public Object getItem(int position) { //返回表的index位置的元组
// TODO Auto-generated method stub
return friend_List.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
this_ID = position;
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = View.inflate(MainActivity.this, R.layout.item, null);
TextView tv_item_name = (TextView) view.findViewById(R.id.tv_item_name);
TextView tv_item_tel = (TextView) view.findViewById(R.id.tv_item_tel);
tv_item_name.setText(friend_List.get(position).getName());
tv_item_tel.setText(friend_List.get(position).getTel());
return view;
}
}
//------------------
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) { //添加菜单的事件
// TODO Auto-generated method stub
int itemId = item.getItemId();
switch (itemId) {
case R.id.menu_add:
addItem();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
private void addItem() { //获得添加事件的方法
// TODO Auto-generated method stub
final View view = View.inflate(MainActivity.this, R.layout.add, null);
Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("添加新记录");
builder.setView(view);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
EditText et_add_name = (EditText)view.findViewById(R.id.et_add_name);
EditText et_add_tel = (EditText)view.findViewById(R.id.et_add_tel);
String name = et_add_name.getText().toString().trim();
String tel = et_add_tel.getText().toString().trim();
friend_List.add(new Person(name,tel));
myAdapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("取消", null);
builder.create().show();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, //设置上下文菜单的监听
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.context, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
int itemId = item.getItemId();
switch (itemId) {
case R.id.menu_delete:
deleteItem();
break;
default:
break;
}
return super.onContextItemSelected(item);
}
private void deleteItem() {
// TODO Auto-generated method stub
Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("删除记录");
builder.setMessage("确定要删除该记录吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
friend_List.remove(this_ID);
myAdapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("取消", null);
builder.create().show();
}
}
<?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="vertical" >
<EditText android:id="@+id/et_add_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" >
<requestFocus />
</EditText>
<EditText android:id="@+id/et_add_tel" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" />
</LinearLayout>
代码时在之前listview那个代码基础上添加的,部分重复代码省略
效果: