public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("选项");
builder.setItems(R.array.choice, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch(which) {
case 0:
break;
case 1:
break;
case 2:
break;
} }
});
builder.setNegativeButton("取消", null);
builder.create().show(); }

string.xml

 <?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">AppExplorer</string>
<string name="hello_world">Hello world!</string>
<array name ="choice">
<item name="start">启动程序</item>
<item name="detail">详细信息</item>
<item name="uninstall">卸载</item>
</array>
</resources>

效果:

AlertDialog的写法-LMLPHP

 public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
final PackageInfo tempPkgInfo = showPackageInfos.get(position);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("选项");
builder.setItems(R.array.choice, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch(which) {
case 0:
String packageName = tempPkgInfo.packageName;
ActivityInfo[] activityInfo = tempPkgInfo.activities;
String activityName = activityInfo[0].name;
try {
Intent i = new Intent();
i.setComponent(new ComponentName(packageName, activityName));
startActivity(i);
} catch (Exception e) {
return;
}
break;
case 1:
showAppDetail(tempPkgInfo);
break;
case 2:
Uri packageUri = Uri.parse("package:" + tempPkgInfo.packageName);
Intent deleteIntent = new Intent();
deleteIntent.setAction(Intent.ACTION_DELETE);
deleteIntent.setData(packageUri);
startActivity(deleteIntent);
break;
} } });
builder.setNegativeButton("取消", null);
builder.create().show(); }

passwordDialog = builder.create();此方法返回一个AlertDialog

05-11 03:08