利用动态布局做的问卷答题demo

     最近一个新需求,做问卷的答题,当点击提交的时候,如果有未答的题目,就自动定位的那道题,以下是我根据需求写的一个demo,我是利用动态布局来做的,希望有对需要的朋友一些帮助, 如有不足的地方,请大神指出,共同进步!!!

public class MainActivity extends Activity {

	private static final String TAG = "MainActivity";
	private LinearLayout lly_test;
	private LayoutInflater mInflater;
	private LinearLayout answer_layout;
	private LinearLayout answer_add; 
	private ImageView answer_image;
	private TextView answer_tv;
	private LinearLayout lly_answer;
	private View question_view; //问题在的view
	final boolean[] statues = new boolean[4]; //选中状态
	private ArrayList<ImageView> imageList; //存每个问题下的imageview  
	//保存图片数组
    private ArrayList<ArrayList<ImageView>> imglist=new ArrayList<ArrayList<ImageView>>();
    private ArrayList<AnswerBean> answer_list;
	private Button submit;
	private ArrayList<QuestionBean> questionList;
	private ScrollView scr_view;
	private LinearLayout layout;
    

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		submit = (Button) findViewById(R.id.submit);
		
		initDate();
		submit.setOnClickListener(new SubmitDateListener(questionList));
	}

	
	private void initDate() {
		AnswerBean mAnswerBean1 = new AnswerBean();
		mAnswerBean1.setAnswer_text("java");
		mAnswerBean1.setAnswer_state("0");
		AnswerBean mAnswerBean2 = new AnswerBean();
		mAnswerBean2.setAnswer_text("c#北京你好, 哈喽,大家好才是真的好, 对的,他好我也好,哈哈哈哈哈哈哈哈哈哈哈,呵呵呵呵呵呵和呵呵呵");
		mAnswerBean2.setAnswer_state("0");
		AnswerBean mAnswerBean3 = new AnswerBean();
		mAnswerBean3.setAnswer_text("c++");
		mAnswerBean3.setAnswer_state("0");
		ArrayList<AnswerBean> answerList1 = new ArrayList<AnswerBean>();
		answerList1.add(mAnswerBean1);
		answerList1.add(mAnswerBean1);
		answerList1.add(mAnswerBean1);
		ArrayList<AnswerBean> answerList2 = new ArrayList<AnswerBean>();
		answerList2.add(mAnswerBean1);
		answerList2.add(mAnswerBean2);
		answerList2.add(mAnswerBean2);
		ArrayList<AnswerBean> answerList3 = new ArrayList<AnswerBean>();
		answerList3.add(mAnswerBean1);
		
		QuestionBean mQuestionBean1 = new QuestionBean();
		mQuestionBean1.setQuestion_state("0");
		mQuestionBean1.setQuestion_text("第一题:");
		mQuestionBean1.setQuestion_id("01");
		mQuestionBean1.setQuestion_answerlist(answerList1);
		QuestionBean mQuestionBean2 = new QuestionBean();
		mQuestionBean2.setQuestion_state("0");
		mQuestionBean2.setQuestion_text("第二题:");
		mQuestionBean2.setQuestion_id("02");
		mQuestionBean2.setQuestion_answerlist(answerList2);
		QuestionBean mQuestionBean3 = new QuestionBean();
		mQuestionBean3.setQuestion_state("0");
		mQuestionBean3.setQuestion_text("第三题:");
		mQuestionBean3.setQuestion_id("03");
		mQuestionBean3.setQuestion_answerlist(answerList3);
		
		questionList = new ArrayList<QuestionBean>();
		questionList.add(mQuestionBean1);
		questionList.add(mQuestionBean2);
		questionList.add(mQuestionBean3);
		
		initView(questionList);
	}

	private void initView(ArrayList<QuestionBean> questionList) {
		lly_test = (LinearLayout) findViewById(R.id.lly_test);
		scr_view = (ScrollView) findViewById(R.id.scr_view);
		layout = new LinearLayout(getApplicationContext());
		layout.setOrientation(LinearLayout.VERTICAL);
		TextView textView;
		for ( int i = 0; i < questionList.size(); i++) {
			question_view = mInflater.inflate(R.layout.question, null);
			textView = (TextView) question_view.findViewById(R.id.tv_question);
			textView.setText(i + "---Android"+"---"+questionList.get(i).getQuestion_text());
			textView.setTextColor(Color.BLACK);
			answer_add = (LinearLayout) question_view.findViewById(R.id.ll_answer); //获取到答案要添加到的view
			int height = answer_add.getHeight(); //获取每一个题目的高度
			imageList =  new ArrayList<ImageView>();
			questionList.get(i).setQuestion_state("0");
			answer_list = questionList.get(i).getQuestion_answerlist();
			
			Log.d(TAG, "fanbingfeng the list is---"+ answer_list.size());
			for (int j = 0; j < answer_list.size(); j++) {
				answer_layout = (LinearLayout) mInflater.inflate(R.layout.answer, null);
				lly_answer = (LinearLayout) answer_layout.findViewById(R.id.lly_answer);
				answer_image = (ImageView) lly_answer.findViewById(R.id.answer_iv);
				answer_tv = (TextView) lly_answer.findViewById(R.id.answer_tv);
				answer_tv.setText("安卓" + j+"--"+answer_list.get(j).getAnswer_text());
//				answer_tv.setPadding(10, 20, 10, 10);
				imageList.add(answer_image);
				lly_answer.setOnClickListener(new AnswerItem(i,j,questionList));
				answer_add.addView(answer_layout);
			}
			imglist.add(imageList);
			layout.addView(question_view);
		}
		TextView tv = new TextView(getApplicationContext());
		tv.setText("先生:");
		float px = getResources().getDisplayMetrics().density;
		tv.setPadding((int)(100/px), 5, 0, 5);
		tv.setTextColor(Color.BLACK);
		lly_test.addView(tv);
		lly_test.addView(layout);
		

	}
	
	class AnswerItem implements OnClickListener{
		private int i;
		private int j;
		private ArrayList<QuestionBean> questionList;
		
		public AnswerItem(int i,int j,ArrayList<QuestionBean> questionList){
			this.i = i;
			this.j = j;
			this.questionList = questionList;
		}
		@Override
		public void onClick(View v) {
			for(int a = 0;a<questionList.get(i).getQuestion_answerlist().size();a++){
				imglist.get(i).get(a).setImageResource(R.drawable.unselected);
				questionList.get(i).getQuestion_answerlist().get(a).setAnswer_state("0");
			}
			if("0".equals(questionList.get(i).getQuestion_answerlist().get(j).getAnswer_state())){ //置为选中
				imglist.get(i).get(j).setImageResource(R.drawable.selected);
				questionList.get(i).getQuestion_answerlist().get(j).setAnswer_state("1");
				questionList.get(i).setQuestion_state("1");
				for(int a = 0;a<questionList.get(i).getQuestion_answerlist().size();a++){
					layout.getChildAt(i).setBackground(null);
				}
			}else{
				imglist.get(i).get(j).setImageResource(R.drawable.unselected);
				questionList.get(i).getQuestion_answerlist().get(j).setAnswer_state("0");
				questionList.get(i).setQuestion_state("0");
			}
		}
	}
	
	
	class SubmitDateListener implements OnClickListener{
		private ArrayList<QuestionBean> questionList;
		public SubmitDateListener(ArrayList<QuestionBean> questionList) {
			this.questionList = questionList;
		}

		@Override
		public void onClick(View v) {
			for(int i=0;i<questionList.size();i++){
				for(int a = 0;a<questionList.get(i).getQuestion_answerlist().size();a++){
					layout.getChildAt(i).setBackground(null);
				}
				if("0".equals(questionList.get(i).getQuestion_state())){ //没答
					int width= layout.getChildAt(i).getWidth();
					int height= (int) layout.getChildAt(i).getY();
					layout.getChildAt(i).setBackground(getResources().getDrawable(R.drawable.rectangle));
					scr_view.smoothScrollTo(width, height);
					Log.d(TAG, "fanbingfeng the unselected view height is--"+height);
					return;
				}
			}
			Toast.makeText(getApplicationContext(), "答题完毕",Toast.LENGTH_SHORT).show();
			
			
		}
		
	}
}

 

转载于:https://my.oschina.net/fbf8866/blog/727921

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值