1. 程式人生 > >自定義控制元件dialog的重要部分程式碼修改字型為例

自定義控制元件dialog的重要部分程式碼修改字型為例

int charactersize = (Integer)SpUtils.get(SettingActivity.this, "fontSize", -1);
		if (charactersize==1) {
			tv_textsizeshow.setText("小");
		}else if (charactersize==2) {
			tv_textsizeshow.setText("中");
		}else if (charactersize==3) {
			tv_textsizeshow.setText("大");
		}else if (charactersize==4) {
			tv_textsizeshow.setText("特大");
		}
		textsizestring = tv_textsizeshow.getText().toString();
public void showTextsize(final List<String> mList) {
		if (textsizeList != null) {
			textsizeList.clear();
		}
		for(int i=0;i<4;i++){
			textsizeList.add(textsizes[i]);
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
		View view = View.inflate(SettingActivity.this, R.layout.set_textsize_change, null);
		builder.setView(view);
		dialog = builder.show();
		final ListView listView = (ListView) view.findViewById(R.id.usercenter_lv);
		TextView cancleTv = (TextView) view.findViewById(R.id.usercenter_tv_cancel);
		View line = (View) view.findViewById(R.id.view_line);
		MyAdapter adapter = new MyAdapter(SettingActivity.this, mList);
		listView.setAdapter(adapter);
		Log.d("zhsy", "textsizePosition==="+textsizePosition);
		if (!TextUtils.isEmpty(textsizestring)) {
			textsizePosition = getTextsizeId(textsizestring, textsizeList);
		}
		if (textsizePosition != -1) {
			adapter.setCurrentId(textsizePosition);
			
		}
		dialog.show();
		cancleTv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
			
		});
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				textsizePosition = position;
				for (int x = 0; x < parent.getCount(); x++) {
					View v = parent.getChildAt(x);
					CheckBox textView = (CheckBox) v.findViewById(R.id.usercenter_item_checkbox);
					if (position == x) {
						textView.setChecked(true);
					} else {
						textView.setChecked(false);
					}
				}
				if (position==0) {
					SpUtils.put(SettingActivity.this, "fontSize", 1);
				}else if (position==1) {
					SpUtils.put(SettingActivity.this, "fontSize", 2);
				}else if (position==2) {
					SpUtils.put(SettingActivity.this, "fontSize", 3);
				}else if (position==3) {
					SpUtils.put(SettingActivity.this, "fontSize", 4);
				}
				textsizestring = mList.get(position);
				Intent intent = getIntent();
				overridePendingTransition(0, 0);
				intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				finish();
				overridePendingTransition(0, 0);
				startActivity(intent);
				dialog.dismiss();
			}
			
		});
		
	}
	private int getTextsizeId(String tsString, List<String> mList) {
		for (int i = 0; i < mList.size(); i++) {
			if (tsString.equals(mList.get(i))) {
				return i;
			}
		}
		return -1;
	}
	class MyAdapter extends BaseAdapter {
		private List<String> mList;
		private Context context;
		private int currentId = -1;

		public MyAdapter(Context context, List<String> mList) {
			this.mList = mList;
			this.context = context;
		}

		public int getCurrentId() {
			return currentId;
		}

		public void setCurrentId(int currentId) {
			this.currentId = currentId;
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			if (mList != null) {
				return mList.size();
			}
			return 0;
		}

		@Override
		public Object getItem(int position) {
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		public void setClick(int pos) {
			if (pos >= 0) {

			}
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			convertView = View.inflate(context, R.layout.usercenter_popview_item, null);
			TextView textView = (TextView) convertView.findViewById(R.id.usercenter_item_tv);
			CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.usercenter_item_checkbox);
			String string = mList.get(position);
			textView.setText(string);
				if (currentId >= 0 && currentId < mList.size()) {
					if (position == currentId) {
						checkBox.setChecked(true);
					} else {
						checkBox.setChecked(false);
					}
				}
				checkBox.setBackgroundResource(R.drawable.checkbox_style);
			return convertView;
		}
	}