1 package com.example.nanchen.cropimagetest; android.app.Activity; 4 import android.app.Dialog; 5 import android.os.Bundle; 6 import android.text.TextUtils; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.view.ViewGroup; 11 import android.view.ViewGroup.LayoutParams; 12 import android.view.Window; 13 import android.view.WindowManager; 14 import android.widget.AdapterView; 15 import android.widget.AdapterView.OnItemClickListener; 16 import android.widget.BaseAdapter; 17 import android.widget.Button; 18 import android.widget.ListView; 19 import android.widget.TextView; java.util.List; * dialog 26 * SelectDialog extends Dialog implements OnClickListener,OnItemClickListener { 29 private SelectDialogListener mListener; 30 private Activity mActivity; 31 private Button mMBtn_Cancel; 32 private TextView mTv_Title; 33 private List<String> mName; 34 private String mTitle; mUseCustomColor = false; mFirstItemColor; mOtherItemColor; SelectDialogListener { onItemClick(AdapterView<?> parent, View view, int position, long id); 41 } SelectDialogCancelListener mCancelListener; SelectDialogCancelListener { onCancelClick(View v); 48 } SelectDialog(Activity activity, int theme, 51 SelectDialogListener listener,List<String> names) { 52 super(activity, theme); 53 mActivity = activity; 54 mListener = listener; 55 this.mName=names; setCanceledOnTouchOutside(true); 59 } activity µ÷Óõ¯³ö²Ëµ¥µÄactivity theme Ö÷Ìâ listener ²Ëµ¥Ïîµ¥»÷ʼþ cancelListener È¡Ïûʼþ names ²Ëµ¥ÏîÃû³Æ 67 * SelectDialog(Activity activity, int theme,SelectDialogListener listener,SelectDialogCancelListener cancelListener ,List<String> names) { 70 super(activity, theme); 71 mActivity = activity; 72 mListener = listener; 73 mCancelListener = cancelListener; 74 this.mName=names; setCanceledOnTouchOutside(false); 78 } activity µ÷Óõ¯³ö²Ëµ¥µÄactivity theme Ö÷Ìâ listener ²Ëµ¥Ïîµ¥»÷ʼþ names ²Ëµ¥ÏîÃû³Æ title ²Ëµ¥±êÌâÎÄ×Ö 86 * SelectDialog(Activity activity, int theme,SelectDialogListener listener,List<String> names,String title) { 89 super(activity, theme); 90 mActivity = activity; 91 mListener = listener; 92 this.mName=names; 93 mTitle = title; setCanceledOnTouchOutside(true); 97 } SelectDialog(Activity activity, int theme,SelectDialogListener listener,SelectDialogCancelListener cancelListener,List<String> names,String title) { 100 super(activity, theme); 101 mActivity = activity; 102 mListener = listener; 103 mCancelListener = cancelListener; 104 this.mName=names; 105 mTitle = title; setCanceledOnTouchOutside(true); 109 } 110 111 @Override onCreate(Bundle savedInstanceState) { 113 super.onCreate(savedInstanceState); 114 View view = getLayoutInflater().inflate(R.layout.view_dialog_select, 115 null); 116 setContentView(view, new LayoutParams(LayoutParams.FILL_PARENT, 117 LayoutParams.WRAP_CONTENT)); 118 Window window = getWindow(); window.setWindowAnimations(R.style.main_menu_animstyle); 121 WindowManager.LayoutParams wl = window.getAttributes(); 122 wl.x = 0; 123 wl.y = mActivity.getWindowManager().getDefaultDisplay().getHeight(); wl.width = LayoutParams.MATCH_PARENT; 126 wl.height = LayoutParams.WRAP_CONTENT; onWindowAttributesChanged(wl); initViews(); 133 } initViews() { 136 DialogAdapter dialogAdapter=new DialogAdapter(mName); 137 ListView dialogList=(ListView) findViewById(R.id.dialog_list); 138 dialogList.setOnItemClickListener(this); 139 dialogList.setAdapter(dialogAdapter); 140 mMBtn_Cancel = (Button) findViewById(R.id.mBtn_Cancel); 141 mTv_Title = (TextView) findViewById(R.id.mTv_Title); mMBtn_Cancel.setOnClickListener(new View.OnClickListener() { 146 147 @Override onClick(View v) { (mCancelListener != null){ 151 mCancelListener.onCancelClick(v); 152 } 153 dismiss(); 154 } 155 }); (!TextUtils.isEmpty(mTitle) && mTv_Title != null){ 158 mTv_Title.setVisibility(View.VISIBLE); 159 mTv_Title.setText(mTitle); 160 }else{ 161 mTv_Title.setVisibility(View.GONE); 162 } 163 } 164 165 @Override onClick(View v) { 167 dismiss(); 168 169 } 170 171 @Override onItemClick(AdapterView<?> parent, View view, int position, 173 long id) { 174 175 mListener.onItemClick(parent, view, position, id); 176 dismiss(); 177 } DialogAdapter extends BaseAdapter { 179 private List<String> mStrings; 180 private Viewholder viewholder; 181 private LayoutInflater layoutInflater; 182 public DialogAdapter(List<String> strings) { 183 this.mStrings = strings; 184 this.layoutInflater=mActivity.getLayoutInflater(); 185 } 186 187 @Override getCount() { mStrings.size(); 191 } 192 193 @Override 194 public Object getItem(int position) { mStrings.get(position); 197 } 198 199 @Override getItemId(int position) { position; 203 } 204 205 @Override 206 public View getView(int position, View convertView, ViewGroup parent) { 207 if (null == convertView) { 208 viewholder=new Viewholder(); 209 convertView=layoutInflater.inflate(R.layout.view_dialog_item, null); 210 viewholder.dialogItemButton=(TextView) convertView.findViewById(R.id.dialog_item_bt); 211 convertView.setTag(viewholder); 212 }else{ 213 viewholder=(Viewholder) convertView.getTag(); 214 } 215 viewholder.dialogItemButton.setText(mStrings.get(position)); 216 if (!mUseCustomColor) { 217 mFirstItemColor = mActivity.getResources().getColor(R.color.dialog_blue); 218 mOtherItemColor = mActivity.getResources().getColor(R.color.dialog_blue); 219 } 220 if (1 == mStrings.size()) { 221 viewholder.dialogItemButton.setTextColor(mFirstItemColor); 222 viewholder.dialogItemButton.setBackgroundResource(R.drawable.dialog_item_bg_only); 223 } else if (position == 0) { 224 viewholder.dialogItemButton.setTextColor(mFirstItemColor); 225 viewholder.dialogItemButton.setBackgroundResource(R.drawable.dialog_item_bg_top); 226 } else if (position == mStrings.size() - 1) { 227 viewholder.dialogItemButton.setTextColor(mOtherItemColor); 228 viewholder.dialogItemButton.setBackgroundResource(R.drawable.dialog_item_bg_buttom); 229 } else { 230 viewholder.dialogItemButton.setTextColor(mOtherItemColor); 231 viewholder.dialogItemButton.setBackgroundResource(R.drawable.dialog_item_bg_center); 232 } 233 return convertView; 234 } 235 236 } Viewholder { 239 public TextView dialogItemButton; 240 } * ÉèÖÃÁбíÏîµÄÎı¾ÑÕÉ« setItemColor(int firstItemColor, int otherItemColor) { 246 mFirstItemColor = firstItemColor; 247 mOtherItemColor = otherItemColor; 248 mUseCustomColor = true; 249 } 250 251 }
ÓÉÓÚͼƬÎļþÒ»¶¨ÔÚÏà²áÖУ¬ÓпÉÄÜÄãÒ²»áÔÚÎļþϵͳÖÐÆäËûµØ·½Ñ¡Ôñ£¬ÕâÀïÎÒ²ÉÓÃ֮ǰдµÄÍòÄÜÊÊÅäÆ÷£¡
ÓÉÓÚÂ¥Ö÷ʱ¼äµÄÈ·±È½Ï½ô£¬ËùÒÔ´úÂ붼ÊÇÖ±½ÓcopyÉÏÀ´µÄ£¬ÓÐЩµØ·½Ã»×öÍêÈ«ÓÅ»¯»¹Íû´ó¼Ò¼ûÁ£¡
ÔÙ¿´¿´ActivityµÄ´úÂëºÍ²¼¾Ö
¡¡