听说是用removeDialog(id),但是我创建的时候不可以指定窗口的id啊, 而且该类也没有finish,dismiss,cancel等方法,麻烦了我的代码如下:
final View selectplate = inflater.inflate(R.layout.selectplateforchangecolor, null);
builder = new AlertDialog.Builder(STKActivity.this).setView(selectplate);
plateList = (ListView)selectplate.findViewById(R.id.plateList1);
builder.setTitle("选择板块");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//这里添加点击确定后的逻辑
logo = 1;
cPage = 1;
total = plateStockRelationService.CountStocksByPlateName(selectedPlateName);
pageBean = new PageBean(cPage,total,pSize);
freshStockList(pageBean);
}
}).setNegativeButton("取消",null).create().show();
}
final View selectplate = inflater.inflate(R.layout.selectplateforchangecolor, null);
builder = new AlertDialog.Builder(STKActivity.this).setView(selectplate);
plateList = (ListView)selectplate.findViewById(R.id.plateList1);
builder.setTitle("选择板块");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//这里添加点击确定后的逻辑
logo = 1;
cPage = 1;
total = plateStockRelationService.CountStocksByPlateName(selectedPlateName);
pageBean = new PageBean(cPage,total,pSize);
freshStockList(pageBean);
}
}).setNegativeButton("取消",null).create().show();
}
解决方案 »
- Android4.1——屏幕旋转的问题
- 要平铺图片,兼用布局让文字浮在图片上,用gridview发现高度太高,无法让字正常显示,类似小米的主题选择页面的平铺是用什么做的?table?
- Android调用系统自带播放器问题
- 请问android开发 如何做软件升级呀?
- 听我老板说java可能10年后就消失了,android什么时候消失
- 求助!关于Android Bluetooth
- android中不同的listview可以用同一个adapter吗?
- listView setAdapter()时 数据绑定报错
- eclipse导入android项目的问题
- 2048中每个数字的滑动效果如何实现
- android 加密有人懂吗
- 写入sd卡遇到问题
final View dialogView = inflater.inflate(
R.layout.dialog_view, null);
builder = new AlertDialog.Builder(STKActivity.this).setView(dialogView);
builder.setTitle("更多操作");
toPlateSet = (Button)dialogView.findViewById(R.id.toPlateSet);
Button toStockGroup = (Button)dialogView.findViewById(R.id.toStockGroup);
toPlateSet.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent toPlateSetIntent = new Intent();
toPlateSetIntent.setClass(STKActivity.this, UserPlateSetActivity.class);
startActivity(toPlateSetIntent);
// 此处用removeDialog(id);id怎么设定?
STKActivity.this.finish();
}
});
toStockGroup.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent toPlateSetIntent = new Intent();
toPlateSetIntent.setClass(STKActivity.this, PlateCombinationOpertion.class);
startActivity(toPlateSetIntent);
removeDialog(1);
STKActivity.this.finish();
}
});
builder.setNeutralButton("取消", null)
.create();
showDialog(1);
builder.show();
}
这里onclick没有第二个参数。而且我也并不是在"确定","取消"中控制关闭的,因为我不希望点击逻辑按钮后再点击确定,这样用户体验不好。
{
……
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
return new AlertDialog.Builder(STKActivity.this).(这儿自己添加).create();
}
……
}
其实就是覆盖 Activity 的方法 onCreateDialog(int id),返回create()创建的Dialog 。
在某个地方调用 onCreateDialog(),传入的id就是这个 AlertDialog.Builder对话框的id,所以要保存这个id值。然后在某个地方调用 removeDialog(id) 就关闭了这个对话框。
1.关闭dialog
DialogInterface.OnClickListener接口的onClick方法第一个参数即为该接口的应用,调用dialog.dismiss()或cancel方法就可以关闭对话框。另外其实dialog的三个类型按钮只要点击就会关闭dialog。所以你想根据逻辑立即关闭dialog的话,只要在onClick方法内直接return就好。
2.不关闭dialog
因为dialog的三个类型按钮无论如何最终都会发送一个关闭消息儿导致对话框关闭。所以你如果要求手动控制(通过dismiss或cancel方法)来关闭对话框的话,你就需要用反射机制替换AlertDialog的消息处理函数。代码如下:/**
*@title ProcessDialog
*@descript 处理dialog类
*@author Finger
*@date 2011-7-28
*@version V1.0
*/
public class ProcessDialog {
public void processDialog(AlertDialog dialog){
if(dialog == null)
return;
try
{
Field field = dialog.getClass().getDeclaredField("mAlert");
field.setAccessible(true);
//获得mAlert变量的值
Object obj = field.get(dialog);
field = obj.getClass().getDeclaredField("mHandler");
field.setAccessible(true);
//修改mHandler变量的值,使用新的ButtonHandler类
field.set(obj, new ButtonHandler(dialog));
}
catch (Exception e)
{
}
}
/***
*
*@title ButtonHandler
*@descript dialog的Button消息处理类 *@author Finger
*@date 2011-7-28
*@version V1.0
*/
class ButtonHandler extends Handler
{ private WeakReference < DialogInterface > mDialog; public ButtonHandler(DialogInterface dialog)
{
mDialog = new WeakReference < DialogInterface > (dialog);
} @Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case DialogInterface.BUTTON_POSITIVE:
case DialogInterface.BUTTON_NEGATIVE:
case DialogInterface.BUTTON_NEUTRAL:
((DialogInterface.OnClickListener) msg.obj).onClick(mDialog
.get(), msg.what);
break ;
}
}
}
}使用时,创建该类对象,调用processDialog传入要处理的AlertDialog即可。
public void handleMessage(Message msg)
{
mDialog.dissmiss();
}};public void onCreate(...)
{
AlertDialog.Builder builder = new AlertDialog.Builder(...); ....... mDialog = builder.create(); mDialog.show();
}
//获取点击事件对象
public OnClickListener getOnClickListener()
{
return new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
handler.sendEmptyMessage(0);//通过handler通知Dialog关闭
} }
}
}