展开样式答题写法
来源:互联网 发布:农村淘宝推广员 编辑:程序博客网 时间:2024/05/16 14:57
先看看实现的效果:
首先看到的第一反应就是一个ListView或者RecyclerView,但是这个里面要求只能按照顺序答题,也就是说,假设3题没有答,第四题是不能点击的。所以这里我用了LinearLayout模拟了一个ListView。
1、自定义一个View,继承LinearLayout实现ListView的功能
public class SelfTestView extends LinearLayout { private BaseAdapter adapter; public SelfTestView(Context context) { this(context, null); } public SelfTestView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public SelfTestView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public void setAdapter(BaseAdapter adapter){ this.adapter = adapter; for(int i = 0;i<adapter.getCount();i++){ View view = adapter.getView(i,null,this); addView(view); if(i == 0){ startAnswer(false,0); } } } public void startAnswer(boolean isAlreadyAnswer,int currentQuestion){ if(isAlreadyAnswer || adapter.getCount()<=currentQuestion){ return; } View view = getChildAt(currentQuestion); TextView mTextView = (TextView) view.findViewById(R.id.text_question); TextView noneView = (TextView) view.findViewById(R.id.btn_none); TextView littleView = (TextView) view.findViewById(R.id.btn_little); TextView someView = (TextView) view.findViewById(R.id.btn_some); TextView oftenView = (TextView) view.findViewById(R.id.btn_often); TextView alwaysView = (TextView) view.findViewById(R.id.btn_always); mTextView.setTextColor(getResources().getColor(R.color.color_3183e9)); noneView.setBackgroundResource(R.drawable.btn_bg_self_test_can_click); littleView.setBackgroundResource(R.drawable.btn_bg_self_test_can_click); someView.setBackgroundResource(R.drawable.btn_bg_self_test_can_click); oftenView.setBackgroundResource(R.drawable.btn_bg_self_test_can_click); alwaysView.setBackgroundResource(R.drawable.btn_bg_self_test_can_click); noneView.setTextColor(getResources().getColor(R.color.color_3183e9)); littleView.setTextColor(getResources().getColor(R.color.color_3183e9)); someView.setTextColor(getResources().getColor(R.color.color_3183e9)); oftenView.setTextColor(getResources().getColor(R.color.color_3183e9)); alwaysView.setTextColor(getResources().getColor(R.color.color_3183e9)); noneView.setEnabled(true); littleView.setEnabled(true); someView.setEnabled(true); oftenView.setEnabled(true); alwaysView.setEnabled(true); }}
2、既然是是模拟ListView,那就少不了Adapter
public class TestQuestionAdapter extends BaseAdapter { private List<String> questions;//题 private Context mContext; private SelfTestView selfTestView; private TextView mAnswerCountView;//答题进度 private boolean alreadyAnswer[];//纪录题是否已经答过 private TextView mSubmitButton;//提交按钮(因为答完题后,提交按钮由灰色变为蓝色,所以要在这个里面处理) private Map<String, Object> answers;//存放答案的容器 public void setData(Context mContext, List<String> questions, SelfTestView selfTestView, TextView mSubmitButton, TextView answerCount) { this.questions = questions; this.mContext = mContext; this.selfTestView = selfTestView; this.mAnswerCountView = answerCount; this.mSubmitButton = mSubmitButton; alreadyAnswer = new boolean[questions.size()]; answers = new HashMap<>(); mAnswerCountView.setText(0 + "/" + questions.size());//一开始答题进度为0 } @Override public int getCount() { return questions.size(); } @Override public Object getItem(int position) { return questions.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { TestQuestionAdapter.ViewHolder viewHolder = null; if (convertView == null) { viewHolder = new TestQuestionAdapter.ViewHolder(); convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_self_test, parent, false); viewHolder.textView = (TextView) convertView.findViewById(R.id.text_question); viewHolder.mNoneView = (TextView) convertView.findViewById(R.id.btn_none); viewHolder.mLittleView = (TextView) convertView.findViewById(R.id.btn_little); viewHolder.mSomeView = (TextView) convertView.findViewById(R.id.btn_some); viewHolder.mOftenView = (TextView) convertView.findViewById(R.id.btn_often); viewHolder.mAlwaysView = (TextView) convertView.findViewById(R.id.btn_always); convertView.setTag(viewHolder); } else { viewHolder = (TestQuestionAdapter.ViewHolder) convertView.getTag(); } final TextView[] btns = {viewHolder.mNoneView, viewHolder.mLittleView, viewHolder.mSomeView, viewHolder.mOftenView, viewHolder.mAlwaysView}; viewHolder.textView.setText((position + 1) + "." + questions.get(position));//由于题没有给编号,所以这里可以自己添加 viewHolder.mNoneView.setEnabled(false); viewHolder.mLittleView.setEnabled(false); viewHolder.mSomeView.setEnabled(false); viewHolder.mOftenView.setEnabled(false); viewHolder.mAlwaysView.setEnabled(false); viewHolder.mNoneView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("-------->", "none" + position); initBtn(btns, 0); judgeStatus(position); //收集答案 //我这里用Map存放的答案,修改的时候根据key相同直接替换,如果用List和对象的形势,要记得先在list中移除要改的那个题,再添加答案,否则可能答5个题出现8答案的情况 /* answers是list<对象>的情况 if(answers.size()>position){//这个判断是说修改不是新答的题 answers.remove(position);//移除上一次的答案 } answers.add(对象);//重新添加答案 */ answers.put(position + "", "答案id"); } }); viewHolder.mLittleView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("-------->", "little" + position); initBtn(btns, 1); judgeStatus(position); answers.put(position + "", "答案id"); } }); viewHolder.mSomeView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { initBtn(btns, 2); judgeStatus(position); answers.put(position + "", "答案id"); } }); viewHolder.mOftenView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("-------->", "often" + position); initBtn(btns, 3); judgeStatus(position); answers.put(position + "", "答案id"); } }); viewHolder.mAlwaysView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("-------->", "always" + position); initBtn(btns, 4); judgeStatus(position); answers.put(position + "", "答案id"); } }); return convertView; } private void judgeStatus(int position) { //如果新答题,答题进度会修改,否则不修改 if (!alreadyAnswer[position]) { mAnswerCountView.setText((position + 1) + "/" + questions.size()); } //设置题为已答状态 alreadyAnswer[position] = true; //答的题不是最后一个就变下一个题为可答题状态,如果是最后一个就改变提交按钮的状态 if (position != questions.size() - 1) { selfTestView.startAnswer(alreadyAnswer[position + 1], position + 1); } else { changeSubmitButtonStatus(); } } private void changeSubmitButtonStatus() { mSubmitButton.setEnabled(true); mSubmitButton.setBackgroundResource(R.drawable.bg_btn_submit_test_select); } //将答案返回供提交 public Map<String, Object> getAnswers() { return answers; } //选中答案后修改界面上被点击的button背景色 private void initBtn(TextView[] btns, int current) { for (int i = 0; i < btns.length; i++) { if (i == current) { btns[current].setBackgroundResource(R.drawable.btn_bg_self_test_select); btns[current].setTextColor(mContext.getResources().getColor(R.color.color_ffffff)); } else { btns[i].setBackgroundResource(R.drawable.btn_bg_self_test_can_click); btns[i].setTextColor(mContext.getResources().getColor(R.color.color_3183e9)); } } } class ViewHolder { TextView textView; TextView mNoneView; TextView mLittleView; TextView mSomeView; TextView mOftenView; TextView mAlwaysView; }}
3、每个Item的布局
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="200dp" android:background="@drawable/bg_self_test" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:orientation="vertical"> <TextView android:id="@+id/text_question" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="1、你的脸比别人大吗" android:layout_marginTop="24dp" android:layout_marginLeft="14dp" android:layout_marginRight="14dp" android:lineSpacingExtra="5dp" android:textColor="@color/color_666666"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentBottom="true" android:layout_marginBottom="40dp" android:gravity="center"> <View android:layout_width="wrap_content" android:layout_height="50dp" android:layout_weight="1"/> <TextView android:id="@+id/btn_none" android:layout_width="50dp" android:layout_height="50dp" android:gravity="center" android:text="没有" android:textSize="14sp" android:textColor="@color/color_666666" android:clickable="true" android:background="@drawable/btn_bg_self_test_not_click"/> <View android:layout_width="wrap_content" android:layout_height="50dp" android:layout_weight="1"/> <TextView android:id="@+id/btn_little" android:layout_width="50dp" android:layout_height="50dp" android:gravity="center" android:text="很少" android:textSize="14sp" android:textColor="@color/color_666666" android:clickable="true" android:background="@drawable/btn_bg_self_test_not_click"/> <View android:layout_width="wrap_content" android:layout_height="50dp" android:layout_weight="1"/> <TextView android:id="@+id/btn_some" android:layout_width="50dp" android:layout_height="50dp" android:gravity="center" android:text="有时" android:textSize="14sp" android:textColor="@color/color_666666" android:clickable="true" android:background="@drawable/btn_bg_self_test_not_click"/> <View android:layout_width="wrap_content" android:layout_height="50dp" android:layout_weight="1"/> <TextView android:id="@+id/btn_often" android:layout_width="50dp" android:layout_height="50dp" android:gravity="center" android:text="经常" android:textSize="14sp" android:textColor="@color/color_666666" android:clickable="true" android:background="@drawable/btn_bg_self_test_not_click"/> <View android:layout_width="wrap_content" android:layout_height="50dp" android:layout_weight="1"/> <TextView android:id="@+id/btn_always" android:layout_width="50dp" android:layout_height="50dp" android:gravity="center" android:text="总是" android:textSize="14sp" android:textColor="@color/color_666666" android:clickable="true" android:background="@drawable/btn_bg_self_test_not_click"/> <View android:layout_width="wrap_content" android:layout_height="50dp" android:layout_weight="1"/> </LinearLayout></RelativeLayout>
注意: drawable都是自定义的shape
bg_btn_submit_test_select.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="22dp"/> <solid android:color="@color/color_3183e9"/></shape>
bg_self_test.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="3dp"/> <solid android:color="@color/color_ffffff"/> <stroke android:color="@color/color_dddddd" android:width="0.5dp"/></shape>
btn_bg_self_test_can_click.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <stroke android:width="1dp" android:color="@color/color_3183e9" /> <solid android:color="@color/color_ffffff"/></shape>
btn_bg_self_test_not_click.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <stroke android:width="1dp" android:color="@color/color_3183e9" /> <solid android:color="@color/color_ffffff"/></shape>
btn_bg_self_test_select.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="@color/color_3183e9"/></shape>
4、使用
public class TestQuestionAcitivity extends AppCompatActivity { private SelfTestView mSelfTextView; private TextView mSubmitTestView; private TextView mAnswerCountView; private TestQuestionAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_question_acitivity); mSelfTextView = (SelfTestView) findViewById(R.id.self_question_view); mSubmitTestView = (TextView) findViewById(R.id.submit_test); mAnswerCountView = (TextView) findViewById(R.id.text_answer_count); mSubmitTestView.setEnabled(false); adapter = new TestQuestionAdapter(); initDatas(); } private void initDatas() { List<String> list = new ArrayList<>(); for(int i = 0;i<30;i++){ list.add("你手脚经常凉吗?"); } adapter.setData(TestQuestionAcitivity.this, list, mSelfTextView, mSubmitTestView,mAnswerCountView); mSelfTextView.setAdapter(adapter); }}
activity_test_question_acitivity.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_test_question_acitivity" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.trtpre.www.demo.activitys.TestQuestionAcitivity"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.trtpre.www.demo.view.SelfTestView android:id="@+id/self_question_view" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> </com.trtpre.www.demo.view.SelfTestView> <TextView android:id="@+id/submit_test" android:layout_width="280dp" android:layout_height="45dp" android:layout_marginLeft="49dp" android:layout_marginRight="49dp" android:textSize="16sp" android:textColor="@color/color_ffffff" android:text="提交" android:background="@color/color_dddddd" android:layout_gravity="center" android:gravity="center" android:layout_marginTop="50dp" android:layout_marginBottom="60dp" /> </LinearLayout> </ScrollView> <TextView android:id="@+id/text_answer_count" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="center" android:layout_marginBottom="20dp" /></RelativeLayout>
阅读全文
0 0
- 展开样式答题写法
- 折叠式答题样式
- 简单的答题模板样式
- 康托展开&康托逆展开 的写法
- 答题
- 样式表的写法
- Wpf 控件样式写法
- 样式表写法
- placeholder样式写法
- Html样式的基本写法
- 针对网页图标样式写法
- 超实用的样式写法
- 返回当前样式-兼容写法
- 兼容写法获取元素样式
- css 自定义样式 继承写法
- 关于康托展开的用途及写法
- JS可以控制样式的名称写法
- 插入SharePoint Ribbon中的样式写法
- 有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个
- 第7章 Spring MVC的高级技术---跨重定向请求传递数据
- JavaScript函数作用域
- Java23种设计模式详解
- js跨域操作json数据
- 展开样式答题写法
- Android WebView 实现缓存网页数据
- 微信小程序开发,提示“不在以下合法域名列表中,请参考文档”
- Determining What Serial Ports Are Available on a Windows Machine
- 基于微信公众平台的二维码签到系统-----(2)二维码的生成以及内网穿透ngrok代理服务器的使用
- 语音听写与合成--(讯飞语音识别与合成&&百度语音识别)
- CodeVS2490 导弹防御塔 【二分答案】【匈牙利】
- LinuxC语言实现上传功能(curl)
- 上传头像