一、Dialog对话框:
Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("提示");
builder.setMessage("确认退出吗?");
builder.setPositiveButton("确认", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
text_main_info.setText("您点击了:" + which);
}
});
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
text_main_info.setText("您点击了:" + which);
}
});
builder.setNeutralButton("中立", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
text_main_info.setText("您点击了:" + which);
}
});
builder.create().show();
//A. 普通列表
button_main_simpleListDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("请选择颜色:");
final String[] arrColor = new String[] { "红色", "绿色","蓝色" };
// 往对话框中放置普通列表
builder.setItems(arrColor, new OnClickListener() {
// 以下代码是模拟:勾选选项后,文字颜色和文本框背景颜色相应发生改变
@Override
public void onClick(DialogInterface dialog , int which) {
text_main_info.setText(arrColor[which]);
switch (which) {
case 0:
text_main_info.setBackgroundColor(Color.RED);
break;
case 1:
text_main_info.setBackgroundColor(Color.GREEN);
break;
case 2:
text_main_info.setBackgroundColor(Color.BLUE);
break;
default:
break;
}
}
});
builder.show();
}
});
//B. 单选列表
button_main_singleListDialog
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("请选择颜色:");
final String[] arrColor = new String[] { "红色", "绿色","蓝色" };
// 往对话框中放置单选列表
builder.setSingleChoiceItems(arrColor, 0,
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
position = which;//通过这个监听器获取到点击的是第几个
}
});
builder.setPositiveButton("确认", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
text_main_info.setText(arrColor[position]);
switch (position) {
case 0:
text_main_info.setBackgroundColor(Color.RED);
break;
case 1:
text_main_info.setBackgroundColor(Color.GREEN);
break;
case 2:
text_main_info.setBackgroundColor(Color.BLUE);
break;
default:
break;
}
}
});
builder.show();
}
});
//C. 多选列表
button_main_multiListDialog
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("请选择颜色:");
final String[] hobbyArr = new String[] { "看书", "学习","饮食", "爬山", "绘画" };
// 将多选项中每次勾选的结果放到一个pos数组中。
pos = new boolean[hobbyArr.length];
// 往对话框中放置多选列表
builder.setMultiChoiceItems(hobbyArr, null,
new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog,int which, boolean isChecked) {
pos[which] = isChecked;//将每一项被勾选的状态放到一个boolean型的数组中
}
});
// 多选列表项必须要使用确认按钮才能最终获得所有勾选的数据。
builder.setPositiveButton("确认", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
// 将所有的勾选项放到一个字符串中
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pos.length; i++) {
if (pos[i]) {
sb.append(hobbyArr[i]);
sb.append("、");
}
}
text_main_info.setText("您勾选了:" + sb.toString());
}
});
builder.show();
}
});
//D. 带图标的列表dialog
button_main_adapterListDialog
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("手机设置:");
// 定义数据源。以下是模拟数据。真实工作中,有可能是从网络上获取数据,也有可能是从xml配置文件中获取数据。
// 考虑到后期维护,很少在java文件中以定义数组的形式作为适配器的数据来源。
List
View view = null;
Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("管理员登录:");
LayoutInflater mInflater = LayoutInflater.from(MainActivity.this);
view = mInflater.inflate(R.layout.loginform_main, null);
final EditText edit_loginform_username = (EditText) view
.findViewById(R.id.edit_loginform_username);
final EditText edit_loginform_password = (EditText) view
.findViewById(R.id.edit_loginform_password);
builder.setView(view);
builder.setPositiveButton("登录", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String username = edit_loginform_username.getText()
.toString().trim();
String password = edit_loginform_password.getText()
.toString().trim();
if (username.length() < 1 || password.length() < 1) {
text_main_info.setText("用户名或密码不可以为空!");
} else {
StringBuilder sb = new StringBuilder();
sb.append("您的登录信息:");
sb.append("用户名:");
sb.append(username);
sb.append("密码:");
sb.append(password);
text_main_info.setText(sb.toString());
}
}
});
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//此处进行异步任务访问网络等处理。
}
});
builder.create().show();
ProgressDialog pDialog = new ProgressDialog(MainActivity.this);
pDialog.setTitle("提示:");
pDialog.setMessage("数据加载中。。。");
pDialog.show();
// 获取系统当前日期
Calendar calendar = Calendar.getInstance();
DatePickerDialog datePickerDialog = new DatePickerDialog(
MainActivity.this, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view,
int year, int monthOfYear,
int dayOfMonth) {
text_main_info.setText("您选择了:" + year
+ ":" + (monthOfYear + 1) + ":"
+ dayOfMonth);
}
}, calendar.get(Calendar.YEAR), calendar
.get(Calendar.MONTH), calendar
.get(Calendar.DAY_OF_MONTH));
datePickerDialog.setCancelable(true);
datePickerDialog.show();
- showAsAction
="never"
android:title="关于"/>
- android:showAsAction
="never" android:title="文字颜色"> <menu > <group> <item android:id="@+id/font_red" android:title="red" />
-
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.font_20:
text_main_info.setTextSize(20);
break;
case R.id.font_30:
text_main_info.setTextSize(30);
break;
case R.id.font_red:
text_main_info.setTextColor(Color.RED);
break;
case R.id.font_green:
text_main_info.setTextColor(Color.GREEN);
break;
}
return super.onOptionsItemSelected(item);
}
1、上下文菜单与Options Menu最大的不同在于:
2、生成上下文菜单是通过Activity中的onCreateContextMenu()方法:
private TextView text_main_info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text_main_info = (TextView) findViewById(R.id.text_main_info);
this.registerForContextMenu(text_main_info);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.contextmenu_main, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.font_20:
text_main_info.setTextSize(20);
break;
case R.id.font_30:
text_main_info.setTextSize(30);
break;
case R.id.font_red:
text_main_info.setTextColor(Color.RED);
break;
case R.id.font_green:
text_main_info.setTextColor(Color.GREEN);
break;
}
return super.onContextItemSelected(item);
}
publicclass MainActivity extends Activity {
private PopupWindow popupWindow = null;
private View popup_view = null;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
popup_view = getLayoutInflater().inflate(R.layout.popwindow_menu, null);
popupWindow = new PopupWindow(popup_view, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
}
@Override
publicboolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_MENU:
if (popupWindow.isShowing()) {
popupWindow.dismiss();
} else {
popupWindow.showAtLocation(popup_view, Gravity.BOTTOM, 0, 0);
ImageView imageView_popup_back = (ImageView) popup_view
.findViewById(R.id.imageView_popup_back);
imageView_popup_back.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
Intent intent = new Intent();
// intent.setAction(Intent.ACTION_MAIN);
// intent.addCategory(Intent.CATEGORY_HOME);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.HOME");
startActivity(intent);
}
});
ImageView imageView_popup_share = (ImageView) popup_view
.findViewById(R.id.imageView_popup_share);
imageView_popup_share.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel://10086"));
startActivity(intent);
}
});
ImageView imageView_popup_collection = (ImageView) popup_view
.findViewById(R.id.imageView_popup_collection);
imageView_popup_collection
.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:10086"));
intent.putExtra("sms_body", "该吃饭了,下课吧!");
startActivity(intent);
}
});
}
break;
case KeyEvent.KEYCODE_BACK:
if (popupWindow.isShowing()) {
popupWindow.dismiss();
returnfalse;
}
default:
break;
}
returnsuper.onKeyDown(keyCode, event);
}
@Override
publicboolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}
@Override
publicboolean onMenuOpened(int featureId, Menu menu) {
returnfalse;
}
}
publicclass MainActivity extends Activity {
private TextView textView_info;
private List list_title = null;
private List list_icons = null;
private PopupWindow popupWindow = null;
private View popup_view = null;
/*
* private int[] imgIds = new int[] { R.drawable.intercept_list,
* R.drawable.intercept_rule, R.drawable.intercepted_record,
* R.drawable.location, R.drawable.incoming_and_outgoing_setting,
* R.drawable.privacy_manager, R.drawable.ip, R.drawable.dial,
* R.drawable.useful };
*
* private String[] arrTitles = new String[] { "拦截名单", "拦截规则", "拦截记录",
* "归属地查询", "来去电设置", "隐私管理", "IP电话设置", "通讯记录", "常用号码" };
*/
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView_info = (TextView) findViewById(R.id.text_main_info);
list_title = new ArrayList();
TypedArray arrTitles = getResources().obtainTypedArray(R.array.titles);
for (int i = 0; i < arrTitles.length(); i++) {
list_title.add(arrTitles.getString(i));
}
list_icons = new ArrayList();
TypedArray arrIcons = getResources().obtainTypedArray(R.array.icons);
for (int i = 0; i < arrIcons.length(); i++) {
list_icons.add(arrIcons.getDrawable(i));
}
textView_info.setText(list_title.toString());
popup_view = getLayoutInflater().inflate(R.layout.popupwindow_menu,
null);
popup_view.setFocusableInTouchMode(true);
GridView popup_gridView = (GridView) popup_view
.findViewById(R.id.gridView_popup);
popup_gridView.setAdapter(new MyAdapter(list_title, list_icons));
popup_gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
publicvoid onItemClick(AdapterView> arg0, View arg1,
int position, long id) {
textView_info.setText("您点击了:" + position);
}
});
textView_info.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
textView_info.setText("您点击了文字");
}
});
// 方法2的第二步:给popupWindow上的View增加键盘监听事件。目的是:能在再次点击menu按键的时候,让popupWindow消失。
popup_view.setOnKeyListener(new OnKeyListener() {
@Override
publicboolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU && popupWindow.isShowing()
&& event.getAction() == KeyEvent.ACTION_DOWN) {
popupWindow.dismiss();
}
returnfalse;
}
});
popupWindow = new PopupWindow(popup_view, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
}
@Override
publicboolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_MENU:
if (!popupWindow.isShowing()) {
// 解决方法二:
// 方法2的第一步:让popupWindow聚焦。只有聚焦之后,其中的Gridview的每个item才能被监听到点击事件。
popupWindow.setFocusable(true);
popupWindow.showAtLocation(popup_view, Gravity.BOTTOM, 0, 0);
}
returntrue;
default:
break;
}
returnsuper.onKeyDown(keyCode, event);
}
class MyAdapter extends BaseAdapter {
private List list_title = null;
private List list_icons = null;
public MyAdapter(List list_title, List list_icons) {
this.list_title = list_title;
this.list_icons = list_icons;
}
@Override
publicint getCount() {
returnlist_title.size();
}
@Override
public Object getItem(int position) {
returnlist_title.get(position);
}
@Override
publiclong getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mHolder = null;
if (convertView == null) {
mHolder = new ViewHolder();
convertView = getLayoutInflater().inflate(
R.layout.item_gridview, parent, false);
mHolder.textView_item_gridview_title = (TextView) convertView
.findViewById(R.id.textView_item_gridview_title);
mHolder.imageView_item_gridview_icon = (ImageView) convertView
.findViewById(R.id.imageView_item_gridview_icon);
convertView.setTag(mHolder);
} else {
mHolder = (ViewHolder) convertView.getTag();
}
mHolder.textView_item_gridview_title.setText(list_title
.get(position));
mHolder.imageView_item_gridview_icon.setImageDrawable(list_icons
.get(position));
// 解决方法一:
// convertView.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View v) {
// textView_info.setText(v.getTag().toString());
// }
// });
return convertView;
}
class ViewHolder {
private ImageView imageView_item_gridview_icon;
private TextView textView_item_gridview_title;
}
}
@Override
publicboolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
returnfalse;
}
}