热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

展开样式答题写法

先看看实现的效果:首先看到的第一反应就是一个ListView或者RecyclerView,但是这个里面要求只能按照顺序答题,也就是说,假设3题没有答,第四题是不能点击的。所以这里我用

先看看实现的效果:
这里写图片描述

首先看到的第一反应就是一个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
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 questions;//题
private Context mContext;
private SelfTestView selfTestView;
private TextView mAnswerCountView;//答题进度
private boolean alreadyAnswer[];//纪录题是否已经答过
private TextView mSubmitButton;//提交按钮(因为答完题后,提交按钮由灰色变为蓝色,所以要在这个里面处理)
private Map answers;//存放答案的容器

public void setData(Context mContext, List 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 getAnswers() {
return answers;
}

//选中答案后修改界面上被点击的button背景色
private void initBtn(TextView[] btns, int current) {
for (int i = 0; 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的布局


<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


<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


<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


<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


<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


<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 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


<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>

推荐阅读
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • iOS超签签名服务器搭建及其优劣势
    本文介绍了搭建iOS超签签名服务器的原因和优势,包括不掉签、用户可以直接安装不需要信任、体验好等。同时也提到了超签的劣势,即一个证书只能安装100个,成本较高。文章还详细介绍了超签的实现原理,包括用户请求服务器安装mobileconfig文件、服务器调用苹果接口添加udid等步骤。最后,还提到了生成mobileconfig文件和导出AppleWorldwideDeveloperRelationsCertificationAuthority证书的方法。 ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • Android JSON基础,音视频开发进阶指南目录
    Array里面的对象数据是有序的,json字符串最外层是方括号的,方括号:[]解析jsonArray代码try{json字符串最外层是 ... [详细]
  • 本文介绍了如何使用elementui分页组件进行分页功能的改写,只需一行代码即可调用。通过封装分页组件,避免在每个页面都写跳转请求的重复代码。详细的代码示例和使用方法在正文中给出。 ... [详细]
  • 本文介绍了一款名为TimeSelector的Android日期时间选择器,采用了Material Design风格,可以在Android Studio中通过gradle添加依赖来使用,也可以在Eclipse中下载源码使用。文章详细介绍了TimeSelector的构造方法和参数说明,以及如何使用回调函数来处理选取时间后的操作。同时还提供了示例代码和可选的起始时间和结束时间设置。 ... [详细]
author-avatar
菜菜ING
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有