展开全部
这是一个我自己写的类,希望能给你帮助:
类名:AddeleteDialog.javapublic class AddeleteDialog extends Dialog implements View.OnClickListener
{
Context context;
View alertDialogView;
ListView noList, yesList;
ArrayList noArray, exportArray;
ArrayAdapter noAdapter, yesAdapter;
ArrayList> array_controlList;
ArrayList yesArray = new ArrayList();
/**
* 两侧e5a48de588b63231313335323631343130323136353331333337396336ListView可添加删除的自定义Dialog
* @param theme
* Style中自定义的dialog样式
* @param noArray
* 界面左侧显示的尚未添加的项目列表,调用此类后 该传入的Array内容 不会改变
* @param exportArray
* 调用此类后会修改 该传入的Array内容 为右侧已添加的项目列表
*/
@SuppressLint("InflateParams")
@SuppressWarnings({ "deprecation", "unchecked" })
public AddeleteDialog(Context context, int theme, ArrayList noArray, ArrayList exportArray)
{
super(context, theme);
LayoutInflater inflater = this.getLayoutInflater();
alertDialogView = inflater.inflate(R.layout.style_dialog_control, null);
this.setContentView(alertDialogView);
this.context = context;
this.exportArray = exportArray;
this.noArray = (ArrayList) noArray.clone();
widgetInit();
WindowManager.LayoutParams lp = this.getWindow().getAttributes();
WindowManager winManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
lp.height = winManager.getDefaultDisplay().getHeight() * 2 / 3;
lp.width = winManager.getDefaultDisplay().getWidth() - 30;
this.getWindow().setAttributes(lp);
}
private void widgetInit()
{
((Button) alertDialogView.findViewById(R.id.cancel)).setOnClickListener(this);
((Button) alertDialogView.findViewById(R.id.enter)).setOnClickListener(this);
noList = (ListView) alertDialogView.findViewById(R.id.device_no);
noAdapter = new ArrayAdapter(context, R.layout.style_listview_control, noArray);
noList.setAdapter(noAdapter);
yesList = (ListView) alertDialogView.findViewById(R.id.device_yes);
yesAdapter = new ArrayAdapter(context, R.layout.style_listview_control, yesArray);
yesList.setAdapter(yesAdapter);
noList.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id)
{
String check = ((TextView) view).getText().toString();
noArray.remove(check);
yesArray.add(check);
noAdapter.notifyDataSetChanged();
yesAdapter.notifyDataSetChanged();
}
});
yesList.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id)
{
String check = ((TextView) view).getText().toString();
yesArray.remove(check);
noArray.add(check);
yesAdapter.notifyDataSetChanged();
noAdapter.notifyDataSetChanged();
}
});
}
public void onClick(View v)
{
switch (v.getId())
{
case R.id.cancel:
this.dismiss();
break;
case R.id.enter:
for (int i = 0; i
{
exportArray.add(yesArray.get(i));
}
this.dismiss();
break;
default:
break;
}
}
}
布局文件:style_dialog_control.xml<?xml version="1.0" encoding="utf-8"?>
android:id="@+id/RelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#E3E3E3"
android:orientation="vertical" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#DA4E7D" >
android:id="@+id/title_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="待添加设备"
android:textSize="25sp" />
android:id="@+id/title_yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingRight="5dp"
android:text="已添加设备"
android:textSize="25sp" />
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="15"
android:baselineAligned="false"
android:orientation="horizontal"
android:padding="2dp" >
android:id="@+id/device_no"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/style_stroke"
android:padding="1dp" >
android:id="@+id/device_yes"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/style_stroke"
android:padding="1dp" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
android:id="@+id/cancel"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="@drawable/style_button"
android:text="取消"
android:textColor="#FFFFFF"
android:textSize="18sp" />
android:id="@+id/enter"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="@drawable/style_button"
android:singleLine="true"
android:text="确定"
android:textColor="#FFFFFF"
android:textSize="18sp" />
布局文件:style_listview_control.xml<?xml version="1.0" encoding="utf-8"?>
http://schemas.android.com/apk/res/android
"
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center_vertical|center_horizontal"
android:minHeight="32dp"
android:text="TextView"
android:textSize="22sp" />