二阶段团队冲刺(7)
今日实现答题功能以及代码整合
package com.llw.goodt.Fragment; import static com.llw.goodt.Util.QuestionUtils.user_input; import android.graphics.Color; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import com.llw.goodt.R; import com.llw.goodt.Util.QuestionUtils; import java.util.ArrayList; import java.util.List; public class DaTiFragment extends Fragment implements View.OnClickListener{ private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; private static final String ARG_PARAM3 = "param3"; private static final String ARG_PARAM4 = "param4"; private String mParam1; private ArrayList<String> mParam2; private int position = -1; private String jiexi = ""; private TextView tv_question; private ImageView iv_a; private ImageView iv_b; private ImageView iv_c; private ImageView iv_d; private TextView tv_xxa; private TextView tv_xxb; private TextView tv_xxc; private TextView tv_xxd; private Boolean[] abcd = new Boolean[]{false,false,false,false}; private LinearLayout ll_a; private LinearLayout ll_b; private LinearLayout ll_c; private LinearLayout ll_d; private TextView tv_result; private LinearLayout ll_jiexi; private TextView tv_jiexi; public DaTiFragment() { // Required empty public constructor } public void setXuanXiang(int shu){ for (int i = 0; i < 4; i++) { abcd[i] = false; } iv_a.setImageResource(R.drawable.a); iv_b.setImageResource(R.drawable.b); iv_c.setImageResource(R.drawable.c); iv_d.setImageResource(R.drawable.d); if(shu==1) {iv_a.setImageResource(R.drawable.a1) ;judgeAnswer("A");} if(shu==2) {iv_b.setImageResource(R.drawable.b1) ;judgeAnswer("B");} if(shu==3) {iv_c.setImageResource(R.drawable.c1) ;judgeAnswer("C");} if(shu==4) {iv_d.setImageResource(R.drawable.d1) ;judgeAnswer("D");} } public static DaTiFragment newInstance(String param1, ArrayList<String> timuList,int position,String jiexi) { DaTiFragment fragment = new DaTiFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putStringArrayList(ARG_PARAM2,timuList); args.putInt(ARG_PARAM3,position); args.putString(ARG_PARAM4,jiexi); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getStringArrayList(ARG_PARAM2); position = getArguments().getInt(ARG_PARAM3); jiexi = getArguments().getString(ARG_PARAM4); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_da_ti, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); tv_question = view.findViewById(R.id.tv_question); iv_a = view.findViewById(R.id.iv_a); iv_b = view.findViewById(R.id.iv_b); iv_c = view.findViewById(R.id.iv_c); iv_d = view.findViewById(R.id.iv_d); tv_xxa = view.findViewById(R.id.tv_xxa);tv_xxa.setText(mParam2.get(0)); tv_xxb = view.findViewById(R.id.tv_xxb);tv_xxb.setText(mParam2.get(1)); tv_xxc = view.findViewById(R.id.tv_xxc);tv_xxc.setText(mParam2.get(2)); tv_xxd = view.findViewById(R.id.tv_xxd);tv_xxd.setText(mParam2.get(3)); tv_question.setText(mParam1); ll_a = view.findViewById(R.id.ll_a);ll_a.setOnClickListener(this); ll_b = view.findViewById(R.id.ll_b);ll_b.setOnClickListener(this); ll_c = view.findViewById(R.id.ll_c);ll_c.setOnClickListener(this); ll_d = view.findViewById(R.id.ll_d);ll_d.setOnClickListener(this); tv_result = view.findViewById(R.id.tv_result); ll_jiexi = view.findViewById(R.id.ll_jiexi); tv_jiexi = view.findViewById(R.id.tv_jiexi); tv_jiexi.setText(jiexi); ll_jiexi.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { QuestionUtils.user_jiexi[position-1] = !QuestionUtils.user_jiexi[position-1]; if(QuestionUtils.user_jiexi[position-1]){ tv_jiexi.setVisibility(View.VISIBLE); }else{ tv_jiexi.setVisibility(View.GONE); } } }); } @Override public void onClick(View v) { tv_result.setText(""); for (int i = 0; i < 4; i++) { abcd[i] = false; } iv_a.setImageResource(R.drawable.a); iv_b.setImageResource(R.drawable.b); iv_c.setImageResource(R.drawable.c); iv_d.setImageResource(R.drawable.d); switch (v.getId()){ case R.id.ll_a: // 濡傛灉鏈閫変腑 QuestionUtils.user_input[position-1] = "A"; judgeAnswer("A"); iv_a.setImageResource(R.drawable.a1);break; case R.id.ll_b: // 濡傛灉鏈閫変腑 QuestionUtils.user_input[position-1] = "B"; judgeAnswer("B"); iv_b.setImageResource(R.drawable.b1);break; case R.id.ll_c: // 濡傛灉鏈閫変腑 QuestionUtils.user_input[position-1] = "C"; judgeAnswer("C"); iv_c.setImageResource(R.drawable.c1);break; case R.id.ll_d: // 濡傛灉鏈閫変腑 QuestionUtils.user_input[position-1] = "D"; judgeAnswer("D"); iv_d.setImageResource(R.drawable.d1);break; } } public void judgeAnswer(String xuanxiang){ if(xuanxiang.equals(QuestionUtils.answers[position-1])) { int color = Color.parseColor("#428E46"); tv_result.setText("鍥炵瓟姝g‘"); tv_result.setTextColor(color); }else { int color = Color.parseColor("#DC2A1D");tv_result.setText("鍥炵瓟閿欒"); tv_result.setTextColor(color); } } public void setTextOpen() { tv_jiexi.setVisibility(View.VISIBLE); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="45dp" android:layout_height="45dp" android:id="@+id/btn_1" android:text="1" android:textSize="24sp" android:textColor="@color/white" android:background="@drawable/btn_bg_not"/> <Button android:layout_marginLeft="30dp" android:layout_width="45dp" android:layout_height="45dp" android:id="@+id/btn_2" android:text="2" android:textSize="24sp" android:textColor="@color/white" android:background="@drawable/btn_bg_not"/> <Button android:layout_marginLeft="30dp" android:layout_width="45dp" android:layout_height="45dp" android:id="@+id/btn_3" android:text="3" android:textSize="24sp" android:textColor="@color/white" android:background="@drawable/btn_bg_not"/> <Button android:layout_marginLeft="30dp" android:layout_width="45dp" android:layout_height="45dp" android:id="@+id/btn_4" android:text="4" android:textSize="24sp" android:textColor="@color/white" android:background="@drawable/btn_bg_not"/> <Button android:layout_marginLeft="30dp" android:layout_width="45dp" android:layout_height="45dp" android:id="@+id/btn_5" android:text="5" android:textSize="24sp" android:textColor="@color/white" android:background="@drawable/btn_bg_not"/> </LinearLayout> <LinearLayout android:layout_marginTop="30dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="45dp" android:layout_height="45dp" android:id="@+id/btn_6" android:text="6" android:textSize="24sp" android:textColor="@color/white" android:background="@drawable/btn_bg_not"/> <Button android:layout_marginLeft="30dp" android:layout_width="45dp" android:layout_height="45dp" android:id="@+id/btn_7" android:text="7" android:textSize="24sp" android:textColor="@color/white" android:background="@drawable/btn_bg_not"/> <Button android:layout_marginLeft="30dp" android:layout_width="45dp" android:layout_height="45dp" android:id="@+id/btn_8" android:text="8" android:textSize="24sp" android:textColor="@color/white" android:background="@drawable/btn_bg_not"/> <Button android:layout_marginLeft="30dp" android:layout_width="45dp" android:layout_height="45dp" android:id="@+id/btn_9" android:text="9" android:textSize="24sp" android:textColor="@color/white" android:background="@drawable/btn_bg_not"/> <Button android:layout_marginLeft="30dp" android:layout_width="45dp" android:layout_height="45dp" android:id="@+id/btn_10" android:text="10" android:textSize="24sp" android:textColor="@color/white" android:background="@drawable/btn_bg_not"/> </LinearLayout> </LinearLayout>
答题功能顺利实现,并开始代码整合