android如何写一个投票或是表达观点的界面

先上图:




把这些表示观点的view放在一个LinearLayout里:

<LinearLayout
  	      xmlns:android="http://schemas.android.com/apk/res/android"
  	      android:id="@+id/repost_vote_tag_list" 
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:visibility="gone">
      </LinearLayout>

每个Item可以这样来实现:

public class KXTagWidget extends TextView{
	
	String mTagName = "";
	
	/** colors */
	int mBgColor = Color.WHITE;
	int mPressedBgColor = Color.WHITE;
	int mTextColor = Color.WHITE;
	int mPressedTextColor = Color.BLACK;
	
	public KXTagWidget(Context context) {
		super(context);
		setClickable(true);
	}
	
	public KXTagWidget(Context context, AttributeSet attrs) {
		super(context, attrs);
		setClickable(true);
	}
	
	public KXTagWidget(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setClickable(true);
	}
	
	public void setName(String tagName){
		if(tagName != null){
			mTagName = tagName;
			this.setText(mTagName);
		}
	}
	
	public void setBgColor(int bgColor, int pressedBgColor){
		mBgColor = bgColor;
		mPressedBgColor = pressedBgColor;
		this.setBackgroundColor(bgColor);
//		this.set
		
		
	}
	
	public int getmPressedBgColor() {
		return mPressedBgColor;
	}

	public void setmPressedBgColor(int mPressedBgColor) {
		this.mPressedBgColor = mPressedBgColor;
	}

	public int getmPressedTextColor() {
		return mPressedTextColor;
	}

	public void setmPressedTextColor(int mPressedTextColor) {
		this.mPressedTextColor = mPressedTextColor;
	}

	public int getmBgColor() {
		return mBgColor;
	}

	public void setmBgColor(int mBgColor) {
		this.mBgColor = mBgColor;
	}

	public int getmTextColor() {
		return mTextColor;
	}

	public void setmTextColor(int mTextColor) {
		this.mTextColor = mTextColor;
	}

	public void setTextColor(int textColor, int pressedTextColor){
		mTextColor = textColor;
		mPressedTextColor = pressedTextColor;
	}
	
	

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		//加画边框
		Paint paint = new Paint();
		paint.setColor(this.mBgColor);
		
		int width = getWidth();
		int height = getHeight();
		canvas.drawLine(0, 0, width - 1, 0, paint);
		canvas.drawLine(width - 1, 0, width - 1, height , paint);
		canvas.drawLine(width - 1, height -1, 0, height - 1, paint);
		canvas.drawLine(0, height -1 , 0, 0, paint);
	}
	
	public int getMesuredWidth() {
		int mesuredWidth = 0;
		Paint p = this.getPaint();
		float textWidth = p.measureText(mTagName);
		mesuredWidth = (int)textWidth + this.getCompoundPaddingLeft()
			+ this.getCompoundPaddingRight();
			
		return mesuredWidth;
	}
	
}

Activity的实现:

	private static final int VOTE_BAR_IMAGE_NUM = 9;
	private int mVoteBarImage[] = new int[VOTE_BAR_IMAGE_NUM];
	private HashMap<String, View> mVoteControlMap = new HashMap<String, View>();
	private int mTagBgColor[] = new int[14];
	/** tag touch listener */
	private TagOnTouchListener mTagOnTouchListener = null;

	/** tag click listener */
	private TagOnClickListener mTagOnClickListener = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initTagBgColor();

		mTagList.add("导致反对微软");
		mTagList.add("声浪再次掀起的");
		mTagList.add("声势");
		mTagList.add("XXX");
		mTagList.add("YYYYYY");
		mTagList.add("XXXXXXXXXXXXXX");
		mTagList.add("ZZZZZZZZ");
		mTagList.add("其中一些中小规模的公司甚至经常低于国家标准甚至不给赔偿");
		mTagList.add("按照国家劳动法有关规定");
		mTagList.add("即以在诺基亚公司");
		mTagList.add("国家");

		constructViews();
	}

	private void constructViews() {
		// total number of votes
		String text = "一共有"+String.valueOf(11)+"项";
		TextView view = (TextView) this
				.findViewById(R.id.repost_vote_total_num_des);
		view.setText(text);

		// friend's opinion label
		TextView view2 = (TextView) this
				.findViewById(R.id.repost_vote_friend_opinion_label);

		View answerlistView = this.findViewById(R.id.repost_vote_anwser_list);
		View taglistView = this.findViewById(R.id.repost_vote_tag_list);
		View inputView = this.findViewById(R.id.repost_vote_input_layout);
		answerlistView.setVisibility(View.GONE);
		taglistView.setVisibility(View.VISIBLE);
		inputView.setVisibility(View.VISIBLE);
		// tag list view
		constructTagListView();

		// result list view
		// constructResultListView();
	}

	private void initTagBgColor() {

		int i;
		for (i = 0; i < 7; ++i) {
			int r = 231 - i * 18;
			int g = 85 - i * 3;
			int b = 85 - i * 3;
			mTagBgColor[i] = Color.rgb(r, g, b);
		}

		for (i = 7; i < 14; ++i) {
			int c = 118 + (i - 7) * 14;
			mTagBgColor[i] = Color.rgb(c, c, c);
		}

	}

	private ArrayList<String> mTagList = new ArrayList<String>();

	private void constructTagListView() {
		LinearLayout parent = (LinearLayout) findViewById(R.id.repost_vote_tag_list);
		if (mTagList == null) {
			parent.setVisibility(View.GONE);
			return;
		}

		if (mTagList.size() == 0) {
			parent.setVisibility(View.GONE);
			return;
		}

		parent.removeAllViews();

		int screenWidth = getWindowManager().getDefaultDisplay().getWidth();

		float totalWidth = screenWidth - 25.0f;
		int tagTotalNum = Math.min(mTagList.size(), 15);
		/** 可放TAG的剩余空间 */
		float surplus = totalWidth;

		LinearLayout oneLine = null;

		oneLine = new LinearLayout(this);
		LayoutParams params = new LayoutParams();
		params.width = LayoutParams.FILL_PARENT;
		params.height = LayoutParams.WRAP_CONTENT;
		params.bottomMargin = 1;
		oneLine.setLayoutParams(params);

		String tagName = "";

		for (int i = 0; i < tagTotalNum; i++) {
			KXTagWidget oneTag = (KXTagWidget) getLayoutInflater().inflate(
					R.layout.kx_widget_view, null);

			tagName = mTagList.get(i);

			oneTag.setName(tagName);
			oneTag.setTag(i);
			oneTag.setBgColor(selectTagBgColor(i), Color.WHITE);
			oneTag.setOnClickListener(this.mTagOnClickListener);
			this.mTagOnClickListener = new TagOnClickListener();
			this.mTagOnTouchListener = new TagOnTouchListener();
			oneTag.setOnTouchListener(this.mTagOnTouchListener);
			oneTag.setOnClickListener(mTagOnClickListener);
			float viewWidth = oneTag.getMesuredWidth();

			if (surplus > viewWidth) {// 如果surplus剩余可画空间能容得下oneTag,则在该行放下这个tag并重新计算剩余可画空间surplus
				oneLine.addView(oneTag);
				surplus -= viewWidth;
			} else {// 如果容不下就换一行,并把上一行add进去,初始化surplus
				parent.addView(oneLine);
				surplus = totalWidth;
				oneLine = new LinearLayout(this);
				oneLine.setLayoutParams(params);

				oneLine.addView(oneTag);
				surplus -= viewWidth;

			}

		}

		if (null != oneLine) {
			parent.addView(oneLine);
		}

	}

	private int selectTagBgColor(int index) {
		if (index < mTagBgColor.length) {
			return mTagBgColor[index];
		} else {
			return mTagBgColor[mTagBgColor.length - 1];
		}
	}

	private int selectVoteBarImage(int index) {
		int i = index % VOTE_BAR_IMAGE_NUM;
		return mVoteBarImage[i];
	}

	private void initVoteBarImage() {
		mVoteBarImage[0] = R.drawable.votec1;
		mVoteBarImage[1] = R.drawable.votec2;
		mVoteBarImage[2] = R.drawable.votec3;
		mVoteBarImage[3] = R.drawable.votec4;
		mVoteBarImage[4] = R.drawable.votec5;
		mVoteBarImage[5] = R.drawable.votec6;
		mVoteBarImage[6] = R.drawable.votec7;
		mVoteBarImage[7] = R.drawable.votec8;
		mVoteBarImage[8] = R.drawable.votec9;
	}

	private class TagOnTouchListener implements OnTouchListener {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			KXTagWidget tagView = (KXTagWidget) v;

			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN: {
				CharSequence tagName = tagView.getText();
				tagView.setTextColor(tagView.getmPressedTextColor());
				tagView.setBackgroundColor(tagView.getmPressedBgColor());
			}
				break;
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_OUTSIDE:
			case MotionEvent.ACTION_CANCEL: {
				tagView.setTextColor(tagView.getmTextColor());
				tagView.setBackgroundColor(tagView.getmBgColor());
			}
				break;
			default:
				break;
			}
			return false;
		}

	}

	private class TagOnClickListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			Integer tagIndex = (Integer) view.getTag();
			String name = mTagList.get(tagIndex);
			Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show();
		}

	}


代码在http://download.csdn.net/detail/baidu_nod/7711097下载

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Android 登录界面的代码示例: 1. 首先,在 XML 布局文件中定义登录界面的 UI: ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <EditText android:id="@+id/usernameEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Username"/> <EditText android:id="@+id/passwordEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword"/> <Button android:id="@+id/loginButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login"/> </LinearLayout> ``` 2. 接下来,在 Java 代码中实现登录功能: ``` public class LoginActivity extends AppCompatActivity { private EditText mUsernameEditText; private EditText mPasswordEditText; private Button mLoginButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); mUsernameEditText = findViewById(R.id.usernameEditText); mPasswordEditText = findViewById(R.id.passwordEditText); mLoginButton = findViewById(R.id.loginButton); mLoginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String username = mUsernameEditText.getText().toString(); String password = mPasswordEditText.getText().toString(); if (isValidCredentials(username, password)) { // 登录成功 Toast.makeText(LoginActivity.this, "Login success", Toast.LENGTH_SHORT).show(); } else { // 登录失败 Toast.makeText(LoginActivity.this, "Invalid credentials", Toast.LENGTH_SHORT).show(); } } }); } private boolean isValidCredentials(String username, String password) { // 这里可以根据实际需求进行验证逻辑的实现 return "admin".equals(username) && "password".equals(password); } } ``` 以上代码实现了一个简单的登录界面,并且当用户点击登录按钮时,会对输入的用户名和密码进行验证,如果验证通过则提示登录成功,否则提示凭证无效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值