android投票功能,Android使⽤RecyclerView实现投票系统本⽂实例为⼤家分享了Android投票系统的具体代码,供⼤家参考,具体内容如下
⼀、创建⼀个fragment_l⽤来显⽰投票的主页⾯
(1)标题栏使⽤Toolbar
(2)投票区域可以滑动,使⽤RecyclerView实现
vegetables怎么读android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="/apk/res-auto"
android:clickable="true"
android:background="@color/backgroundColorWhite">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/backgroundColorWhite"
android:orientation="vertical">
android:id="@+id/vote_list_toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbarHeight"
android:background="@color/backgroundColorWhite"
app:contentIntStart="0dp">
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/vote_list_back_btn"
android:layout_width="@dimen/titleBarBackWidth"
android:layout_height="@dimen/titleBarBackHeight"
android:layout_margin="@dimen/margin_min"
android:layout_centerVertical="true"
android:background="@drawable/titlebar_back"
android:layout_marginLeft="@dimen/padding_20"
/>
android:id="@+id/vote_list_title_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center_vertical"
美语发音视频教程android:text="投票"samen
android:textColor="@color/textcolor_28282d"
android:textSize="@dimen/textSizeMax"
android:textStyle="bold"/>
android:id="@+id/vote_list_recycleview"
android:layout_width="match_parent"
android:layout_height="match_parent">
2013 考研注:界⾯字体⼤⼩以及控件宽度⾃⾏调整即可,使⽤RecyclerView⾸先需要在项⽬的adle中添加相应的依赖库才⾏。添加:implementation ‘com.android.support:recyclerview-v7:24.2.1'
界⾯效果:
⼆、创建⼀个l⽤来显⽰投票的具体内容
(1)主布局使⽤LinearLayout实现,⾥⾯添加⼀个TextView⽤来显⽰投票的问题,使⽤CheckBox作为投票的多选框。
(2)将当前的Item加载到投票的主页⾯中
android:orientation="vertical"
android:layout_width="match_parent"
rather
android:layout_height="wrap_content"
android:background="@color/backgroundColorWhite"
>
android:id="@+id/item_vote_question_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1.请问你⽀持哪⼀个决议?"
android:textColor="@color/black"
android:textSize="@dimen/item_vote_question"波兰人英语
android:layout_marginLeft="@dimen/padding_20" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/backgroundColorWhite" android:orientation="vertical"
android:layout_margin="@dimen/padding_20"> android:id="@+id/item_vote_answer1_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAAA"
android:textColor="@color/black"
android:textSize="@dimen/item_vote_answer" /> android:id="@+id/item_vote_answer2_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BBBBBBB"
android:textColor="@color/black"
android:textSize="@dimen/item_vote_answer" /> android:id="@+id/item_vote_answer3_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BCCCCC"
android:textColor="@color/black"
android:textSize="@dimen/item_vote_answer" />
界⾯效果:
三、创建⼀个投票信息实体类作为适配器的适配类型,新建VoteInfo.java类。
public class VoteInfo {
private String questionItem;
private String[] answerItems;
afloatpublic VoteInfo(String questionItem,String[] answerItems){
this.questionItem=questionItem;
this.answerItems=answerItems;
}
public String getQuestionItem(){
return questionItem;
}
public String[] getAnswerItems(){
return answerItems;
}
}
四、接下来需要为RecyclerView准备⼀个适配器,新建VoteInfoAdapter.java,让这个适配器继承⾃RecyclerView.Adapter,并将泛型指定为VoteInfoAdapter.ViewHolder。其中,ViewHolder是我们在VoteInfoAdapter中定义的⼀个内部类。
public class VoteInfoAdapter extends RecyclerView.Adapter {
private List mVoteInfoList;
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.Context()).inflate(R.layout.item_vote,parent,fal);
ViewHolder holder=new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
VoteInfo (position);
holder.questionItem.QuestionItem());
holder.answerItem_1.AnswerItems()[0]);
holder.answerItem_2.AnswerItems()[1]);
holder.answerItem_3.AnswerItems()[2]);
}
@Override
public int getItemCount() {
return mVoteInfoList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder{
TextView questionItem;
CheckBox answerItem_1;
CheckBox answerItem_2;
CheckBox answerItem_3;
public ViewHolder(View itemView) {
super(itemView);
questionItem=(TextView)itemView.findViewById(R.id.item_vote_question_tv); answerItem_1=(CheckBox)itemView.findViewById(R.id.item_vote_answer1_cb); answerItem_2=(CheckBox)itemView.findViewById(R.id.item_vote_answer2_cb); answerItem_3=(CheckBox)itemView.findViewById(R.id.item_vote_answer3_cb);
}
}
public VoteInfoAdapter(List voteInfoList){
mVoteInfoList=voteInfoList;
}
}
五、适配器已经准备完毕,开始使⽤RecyclerView,新建⼀个ShowVoteAdapter.java类。public class ShowVoteActivity extends BaActivity{
@BindView(R.id.vote_list_recycleview)
RecyclerView recyclerView;
private List voteInfoList=new ArrayList();
@Override
protected void onCreate(Bundle saveInstanceState) {
技术诀窍Create(saveInstanceState);
ScreenUtils.tContentViewWithOrientation(this,
ScreenUtils.isPhone() ? R.layout.fragment_vote_list : R.layout.fragment_vote_list); initVoteInfo();
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this); recyclerView.tLayoutManager(linearLayoutManager);
复旦附中国际部VoteInfoAdapter voteInfoAdapter=new VoteInfoAdapter(voteInfoList);
8年级英语