android java代码中设置radiobutton选中与为选中颜色

tab_rb_b.setTextColor(MainActivity.this.getResources().getColor(R.drawable.main_text_color));
其中R.drawable.main_text_color是自己写好的 颜色改变的xml


<!-- not selected -->


但是设好之后 颜色并没有随选中而改变,只是一直都是为选中状态的颜色。
哪位大神知道是怎么回事吗?

4个回答

radioButton.setTextColor(getResources().getColorStateList(R.color.selector_filter_text_color));

换成getColorStateList

shenghou12
HouV 就是这个问题,首先要放在res/color下
大约 3 年之前 回复
u010032372
-琥珀川-
3 年多之前 回复

button1.setTextColor(this.getResources().getColorStateList(R.drawable.color_radiobutton));这么做是对的,可以显示选中和未选中的颜色,

同求呀,哥们解决了的话报一下,谢了。。。

在Xml中写好selector 在selector中指定好选中的颜色和未选中的颜色。
然后在radioButton的Xml文件中设置background属性为R.Drawable.selector文件名,
这样就OK了

qq_22108817
天凉好个秋丶 逗比。。。人家说的是用 java 写的 不是 xml 。
大约 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
Python+OpenCV计算机视觉

Python+OpenCV计算机视觉

【Android】自定义RadioButton,请问如何改变选中状态的图片大小?

在点击按钮时,按钮被选中状态的图片要高出一些,请问该如何实现?

如何检测哪个radiogroup按钮被选中了呢?

在radiogroup中有三个radiobuttons,我使用Java如何实现不同按钮被选中会发生不同的动作?下面是关于radiogroup和radiobutton的代码: final RadioGroup size = (RadioGroup)findViewById(R.id.RGSize); final RadioButton small = (RadioButton)findViewById(R.id.RBS); final RadioButton medium = (RadioButton)findViewById(R.id.RBM); final RadioButton large = (RadioButton)findViewById(R.id.RBL); if (size.getCheckedRadioButtonId().equals(small){ } else{ } 但是equals不是正确的语法。如何用Java实现按钮被选中后发生动作的功能?

布局里有多个RadioButton,但是不在一个RadioGroup里,怎么让他们选择的时候互斥呢!

如题,我一个布局里有多个地方用到RadioButton,由于需要,布局复杂导致他们不在一个RadioGroup里,怎么让他们选择的时候互斥呢,有什么好的方法吗,总不是挨个监听设置其他的RadioButton是Checked为false吧。求高手指教

Android-单选按钮的值设定

求助,应用不管选中哪个按钮,价格总是525.00。不知道怎么搞的。 //Get and set package selected. radioPackageGroup = (RadioGroup) findViewById(R.id.radTadPackage); int selectedId = radioPackageGroup.getCheckedRadioButtonId(); btnPackage = (RadioButton) findViewById(selectedId); String display_package_selected = btnPackage.getText().toString(); TadGlobals.getTadPackageInstance().setTadPackage(display_package_selected); //Set package price. double package_price = 0.00; if(display_package_selected == "Bronze"){ package_price = 150.00; }else if(display_package_selected == "Silver"){ package_price = 225.00; }else if(display_package_selected == "Gold"){ package_price = 375.00; }else{ package_price = 525.00; } TadGlobals.getPackagePriceInstance().setPackagePrice(package_price);

求大神解决,安卓中radioButton setChecked(true);空指针错误

public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener{ private RadioGroup rg_tab_bar; private RadioButton channel1; private FragmentManager fManager; private MyFragment1 fg1; private MyFragment2 fg2; private MyFragment3 fg3; private MyFragment4 fg4; private MyFragment5 fg5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rg_tab_bar = (RadioGroup)findViewById(R.id.rg_tab_bar); rg_tab_bar.setOnCheckedChangeListener(this); //获取第一个单选按钮,并设置其为选中状态 channel1=(RadioButton)findViewById(R.id.rb_channel1); channel1.setChecked(true); fManager = getSupportFragmentManager(); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { FragmentTransaction fTransaction = fManager.beginTransaction(); hideAllFragment(fTransaction); switch (checkedId){ case R.id.rb_channel1: if(fg1 == null){ fg1 = new MyFragment1(); fTransaction.add(R.id.ly_content,fg1); }else{ fTransaction.show(fg1); } break; case R.id.rb_channel2: if(fg2 == null){ fg2 = new MyFragment2(); fTransaction.add(R.id.ly_content,fg2); }else{ fTransaction.show(fg2); } break; case R.id.rb_channel3: if(fg3 == null){ fg3 = new MyFragment3(); fTransaction.add(R.id.ly_content,fg3); }else{ fTransaction.show(fg3); } break; case R.id.rb_channel4: if(fg4 == null){ fg4 = new MyFragment4(); fTransaction.add(R.id.ly_content,fg4); }else{ fTransaction.show(fg4); } break; case R.id.rb_channel5: if(fg5 == null){ fg5 = new MyFragment5(); fTransaction.add(R.id.ly_content,fg5); }else{ fTransaction.show(fg5); } break; } fTransaction.commit(); } //隐藏所有Fragment private void hideAllFragment(FragmentTransaction fragmentTransaction){ if(fg1 != null)fragmentTransaction.hide(fg1); if(fg2 != null)fragmentTransaction.hide(fg2); if(fg3 != null)fragmentTransaction.hide(fg3); if(fg4 != null)fragmentTransaction.hide(fg4); if(fg5 != null)fragmentTransaction.hide(fg5); } } Caused by: java.lang.NullPointerException at com.rong.shop.myshop.home.MainActivity.onCheckedChanged(MainActivity.java:43) at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174) at android.widget.RadioGroup.access$600(RadioGroup.java:54) at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:358) at android.widget.CompoundButton.setChecked(CompoundButton.java:130) at com.rong.shop.myshop.home.MainActivity.onCreate(MainActivity.java:34) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)  at android.app.ActivityThread.access$800(ActivityThread.java:144)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:136)  at android.app.ActivityThread.main(ActivityThread.java:5146)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)  at dalvik.system.NativeStart.main(Native Method)  我把channel1=(RadioButton)findViewById(R.id.rb_channel1); channel1.setChecked(true);这两行去掉以切正常 这是什么问题?

如何改变spinner中的数组源

在程序中有一个 spinner5 ,我在 values folder 中创建了两个字符串数组 name_array 和 type_array 。还有两个 radio buttons: radiobtn1 和 radiobtn2 。 我想实现的是当每个单选按钮被选中时改变 spinner5 的内容。 if (radiobtn1 被选中) then spinner5 从 name_array 中显示内容 else If (radiobtn2 被选中) then spinner5 从 type_array 中显示内容 如何实现这个功能呢?

java AS新手求助。以下是代码:

# setOnCheckedChangeListener方法参数类型不匹配 ackage gke_code_jrxd.Demo.ui; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import gke_code_jrxd.Demo.BuildConfig; import gke_code_jrxd.Demo.R; import gke_code_jrxd.Demo.ui.index.FunnyFragment; import gke_code_jrxd.Demo.ui.index.IndexFragment; import gke_code_jrxd.Demo.ui.index.NewsFragment; import gke_code_jrxd.Demo.ui.widget.TitleBar; import butterknife.Bind; import butterknife.ButterKnife; public class MainActivity extends FragmentActivity { @Bind(R.id.titleBar) TitleBar titleBar; @Bind(R.id.rgTabBar) RadioButton rgTabBar; @Bind(R.id.rbTabIndex) RadioButton rbTabIndex; @Bind(R.id.rbTabNews) RadioButton rbTabNews; @Bind(R.id.rbTabFunny) RadioButton rbTabFunny; IndexFragment mIndexFragment = new IndexFragment(); NewsFragment mNewsFragment = new NewsFragment(); FunnyFragment mFunnyFragment = new FunnyFragment(); @Override protected void onCreate(Bundle savedInstanceState) { // this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去除标题栏 super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); titleBar.setTitle("首页"); titleBar.setLeftTextLeftDrawable("", 0); addFragment(); # rgTabBar.setOnCheckedChangeListener(mOnCheckedChangeListener); // rgTabBar.check(R.id.rbTabIndex); // 在这里为什么不用 check(R.id.rbTabIndex)默认选中第一项,是因为y // mOnCheckedChangeListener事件会执行多次,所以弃用它, // 直接让第一个RadioButton设置选中解决该问题 rbTabIndex.setChecked(true); } /** * 首页tab索引 **/ final int TAB_INDEX = 0; /** * 今日新闻tab索引 **/ final int TAB_NEWS = 1; /** * 今日笑点tab索引 **/ final int TAB_FUNNY = 2; /** * 当前展示的tab索引 **/ int mCurShowFragmentIndex; /** * tab选项卡监听器 */ RadioGroup.OnCheckedChangeListener mOnCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() { @Override public void OnCheckedChanged(RadioGroup group,int checkedId) { switch (checkedId) { case R.id.rbTabIndex: changeTab(TAB_INDEX); break; case R.id.rbTabNews: changeTab(TAB_NEWS); break; case R.id.rbTabFunny: changeTab(TAB_FUNNY); break; } } }; public void addFragment() { FragmentTransaction transaction = getSupportFragmentManager() .beginTransaction(); transaction.add(R.id.flContent, mIndexFragment); transaction.add(R.id.flContent, mNewsFragment); transaction.add(R.id.flContent, mFunnyFragment); transaction.commit(); showFragment(TAB_INDEX); } /** * 切换 tab选项卡 * @param tabItem */ public void changeTab(final int tabItem) { switch (tabItem) { case TAB_INDEX: titleBar.setTitle("首页"); mCurShowFragmentIndex = TAB_INDEX; showFragment(TAB_INDEX); break; case TAB_NEWS: titleBar.setTitle("今日新闻"); mCurShowFragmentIndex = TAB_NEWS; showFragment(TAB_NEWS); break; case TAB_FUNNY: titleBar.setTitle("今日笑点"); mCurShowFragmentIndex = TAB_FUNNY; showFragment(TAB_FUNNY); break; } //showFragment(mCurShowFragmentIndex); } /** * hide所有的fragment */ private void hideFragment() { FragmentTransaction transaction = getSupportFragmentManager() .beginTransaction(); transaction.hide(mIndexFragment); transaction.hide(mNewsFragment); transaction.hide(mFunnyFragment); transaction.commit(); } /** * show选中的fragment * * @param tab */ public void showFragment(final int tab) { hideFragment(); FragmentTransaction transaction = getSupportFragmentManager() .beginTransaction(); switch (tab) { case TAB_INDEX: transaction.show(mIndexFragment); break; case TAB_NEWS: transaction.show(mNewsFragment); break; case TAB_FUNNY: transaction.show(mFunnyFragment); break; } transaction.commit(); } }

安卓的 RadioGroup,取值的解决思路

我定义的一个新闻类型 为 Integer 类型。 这是一个 单选按钮组 ![图片说明](https://img-ask.csdn.net/upload/201509/10/1441857372_689404.png) ``` Integer newType = radioType.getCheckedRadioButtonId(); XX news = new XX(); news.setType(newType); ``` 现在数据表里面是 取的 两个数字“0000”,“0001” 我现在要将这数字显示成中文,如图上。 思路是,在上传的时候传中文, 还是取的时候改成中文

点击RecyclerView瀑布流布局中的图片item跳转到下一个activity

![图片说明](https://img-ask.csdn.net/upload/202004/16/1587024417_50235.jpg) 这是activity ``` package com.example.listview.Menu; import android.content.Intent; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import com.example.listview.R; import com.example.listview.Menu.MenuActivity; import com.example.listview.Pay.PayActivity; import com.example.listview.Menu.MenuAdapter; import com.example.listview.table.TMenu; import com.example.listview.Windows; import org.litepal.crud.DataSupport; import java.util.ArrayList; import java.util.List; public class MenuActivity extends AppCompatActivity { private MenuAdapter menuAdapter; private RecyclerView mRv; //private OnItemClickListener mOnItemClickListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.menu_activity); ActionBar actionBar=getSupportActionBar(); if(actionBar!=null){ actionBar.hide(); } initMenus();//初始化数据 RecyclerView recyclerView=(RecyclerView) findViewById(R.id.rv); StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);//P128瀑布流布局 recyclerView.setLayoutManager(layoutManager); MenuAdapter madapter=new MenuAdapter(menuList); recyclerView.setAdapter(madapter); madapter.setOnItemClick(new MenuAdapter.OnItemClickListener() { @Override public void OnItemClick(View v, int position, String id) { Intent intent = new Intent(MenuActivity.this, PayActivity.class); /*传ID在Main2Activity接受*/ /* intent.putExtra("id",id);*/ startActivity(intent); } }); /*adapter.setOnItemClickListener(new MenuAdapter.OnItemClickListener() { @Override public void onItemClick(View view, int position) { //String masterPic = show.getResult().get(position).getMasterPic(); //String name = show.getResult().get(position).getCommodityName(); //String price =show.getResult().get(position).getPrice(); Intent intent = new Intent(MenuActivity.this, PayActivity.class); //intent.putExtra("cid",masterPic); //intent.putExtra("name",name); //intent.putExtra("price",price); startActivity(intent); } });*/ } private List<Menu> menuList =new ArrayList<>(); private void initMenus() { List<TMenu> menus = DataSupport.select("name", "price","store","foodid","picture").find(TMenu.class); for (TMenu menu : menus) { int resID = getResources().getIdentifier(menu.getPicture(), "drawable", "com.example.listview"); Menu canteen=new Menu(menu.getName(),resID); menuList.add(canteen); } } } ``` 这是Adapter ``` package com.example.listview.Menu; /** * Created by Administrator on 2019/4/3 0003. */ import android.content.Context; import android.content.Intent; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.example.listview.R; import com.example.listview.Pay.PayActivity; import com.example.listview.Menu.Menu; import com.example.listview.Menu.MenuAdapter; import java.util.List; /** * Created by Administrator on 2019/2/8 0008. */ public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.ViewHolder>{ private List<Menu> mMenuList; private Context mcontext; private OnItemClickListener mOnItemClickListener; OnItemClickListener listener; public interface OnItemClickListener { public void onItemClick(View v, int position); } public void setOnItemClick(OnItemClickListener listener) { this.listener=listener; } static class ViewHolder extends RecyclerView.ViewHolder { View fruitView; ImageView fruitImage; TextView fruitName; public ViewHolder(View view) { super(view); fruitView = view; fruitImage = (ImageView) view.findViewById(R.id.fruit_image); fruitName = (TextView) view.findViewById(R.id.fruit_name); } } public MenuAdapter(List<Menu> menuList) { mMenuList = menuList; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_item, parent, false); final ViewHolder holder = new ViewHolder(view); holder.fruitView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = holder.getAdapterPosition(); Menu menu = mMenuList.get(position); //Intent intent=new Intent(MenuActivity.this,PayActivity.class); //startActivity(intent); Toast.makeText(v.getContext(), "you clicked view" + menu.getName(), Toast.LENGTH_SHORT).show(); } }); holder.fruitImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //mOnItemClickListener.onItemClick(v, position); //Intent intent=new Intent(); //Intent intent=new Intent(Context,PayActivity.class); //intent.putExtra("friend",msg.getFriend().toString()); //Context.startActivity(intent); //int position = holder.getAdapterPosition(); //Menu menu = mMenuList.get(position); //Toast.makeText(v.getContext(), "you clicked image" + menu.getName(), Toast.LENGTH_SHORT).show(); } }); return holder; } @Override public void onBindViewHolder(ViewHolder holder,int position){ Menu menu = mMenuList.get(position); holder.fruitImage.setImageResource(menu.getImageId()); holder.fruitName.setText(menu.getName()); holder.fruitImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(listener!=null){ /*注意参数*/ listener.OnItemClick(v,position); } } }); } @Override public int getItemCount(){ return mMenuList.size(); } } ```

android空指针问题求助

public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener{ private RadioGroup rg_tab_bar; private RadioButton channel1; private FragmentManager fManager; private MyFragment1 fg1; private MyFragment2 fg2; private MyFragment3 fg3; private MyFragment4 fg4; private MyFragment5 fg5; //ViewPager private ViewPager vpager_one; private ArrayList<View> aList; private AdapterViewPager mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //ViewPager vpager_one=(ViewPager)findViewById(R.id.vpager); aList = new ArrayList<View>(); LayoutInflater li = getLayoutInflater(); aList.add(li.inflate(R.layout.fg_home_viewpager_view_1,vpager_one,true)); aList.add(li.inflate(R.layout.fg_home_viewpager_view_2,vpager_one,true)); aList.add(li.inflate(R.layout.fg_home_viewpager_view_3,vpager_one,true)); aList.add(li.inflate(R.layout.fg_home_viewpager_view_4,vpager_one,true)); aList.add(li.inflate(R.layout.fg_home_viewpager_view_5,vpager_one,true)); mAdapter = new AdapterViewPager(aList); vpager_one.setAdapter(mAdapter);//**这是第56行,报错的位置** rg_tab_bar = (RadioGroup)findViewById(R.id.rg_tab_bar); rg_tab_bar.setOnCheckedChangeListener(this); //获取第一个单选按钮,并设置其为选中状态 channel1=(RadioButton)findViewById(R.id.rb_channel1); channel1.setChecked(true); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { fManager = getSupportFragmentManager(); FragmentTransaction fTransaction = fManager.beginTransaction(); hideAllFragment(fTransaction); switch (checkedId){ case R.id.rb_channel1: if(fg1 == null){ fg1 = new MyFragment1(); fTransaction.add(R.id.ly_content,fg1); }else{ fTransaction.show(fg1); } break; case R.id.rb_channel2: if(fg2 == null){ fg2 = new MyFragment2(); fTransaction.add(R.id.ly_content,fg2); }else{ fTransaction.show(fg2); } break; case R.id.rb_channel3: if(fg3 == null){ fg3 = new MyFragment3(); fTransaction.add(R.id.ly_content,fg3); }else{ fTransaction.show(fg3); } break; case R.id.rb_channel4: if(fg4 == null){ fg4 = new MyFragment4(); fTransaction.add(R.id.ly_content,fg4); }else{ fTransaction.show(fg4); } break; case R.id.rb_channel5: if(fg5 == null){ fg5 = new MyFragment5(); fTransaction.add(R.id.ly_content,fg5); }else{ fTransaction.show(fg5); } break; } fTransaction.commit(); } //隐藏所有Fragment private void hideAllFragment(FragmentTransaction fTransaction){ if(fg1 != null)fTransaction.hide(fg1); if(fg2 != null)fTransaction.hide(fg2); if(fg3 != null)fTransaction.hide(fg3); if(fg4 != null)fTransaction.hide(fg4); if(fg5 != null)fTransaction.hide(fg5); } } Caused by: java.lang.NullPointerException at com.rong.shop.myshop.home.MainActivity.onCreate(MainActivity.java:56)

请问我这个东西错误在哪,看了半天看不出来,求大佬帮我看下

![图片说明](https://img-ask.csdn.net/upload/201711/13/1510560152_438600.png) ![![图片说明](https://img-ask.csdn.net/upload/201711/13/1510560174_49600.png)![图片说明](https://img-ask.csdn.net/upload/201711/13/1510560153_160100.png) ![图片说明](https://img-ask.csdn.net/upload/201711/13/1510560199_737771.png)![图片说明](https://img-ask.csdn.net/upload/201711/13/1510560181_584002.png)![图片说明](https://img-ask.csdn.net/upload/201711/13/1510560285_644985.png) package com.example.answers; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import android.R.bool; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Checkable; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; /** * 答题系统 */ public class MainActivity extends Activity { // 数据库名称 private String DB_NAME = "question.db"; // 数据库的地址 private String DB_PATH = "/data/data/com.example.answers/databases/"; // 总的题目数据 private int count; // 当前显示的题目 private int corrent; // 问题 private TextView tv_title; // 选项 RadioButton[] mRadioButton = new RadioButton[4]; // 上一题 private Button btn_up; // 下一题 private Button btn_down; // 详情 private TextView tv_result; // 容器 private RadioGroup mRadioGroup; // 是否进入错题模式 private boolean wrongMode; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFile(); initView(); initDB(); } /** * 初始化View */ private void initView() { wrongMode = false; tv_title = (TextView) findViewById(R.id.tv_title); mRadioButton[0] = (RadioButton) findViewById(R.id.RadioA); mRadioButton[1] = (RadioButton) findViewById(R.id.RadioB); mRadioButton[2] = (RadioButton) findViewById(R.id.RadioC); mRadioButton[3] = (RadioButton) findViewById(R.id.RadioD); btn_down = (Button) findViewById(R.id.btn_down); btn_up = (Button) findViewById(R.id.btn_up); tv_result = (TextView) findViewById(R.id.tv_result); mRadioGroup = (RadioGroup) findViewById(R.id.mRadioGroup); } /** * 初始化数据库服务 */ private void initDB() { DBService dbService = new DBService(); final List<Question> list = dbService.getQuestion(); count = list.size(); corrent = 0; Question q = list.get(0); tv_title.setText(q.question); mRadioButton[0].setText(q.answerA); mRadioButton[1].setText(q.answerB); mRadioButton[2].setText(q.answerC); mRadioButton[3].setText(q.answerD); // 上一题 btn_up.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { if (corrent > 0) { corrent--; Question q = list.get(corrent); tv_title.setText(q.question); mRadioButton[0].setText(q.answerA); mRadioButton[1].setText(q.answerB); mRadioButton[2].setText(q.answerC); mRadioButton[3].setText(q.answerD); tv_result.setText(q.explaination); mRadioGroup.clearCheck(); // 设置选中 if (q.selectedAnswer != -1) { mRadioButton[q.selectedAnswer].setChecked(true); } } } }); // 下一题 btn_down.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 判断是否为最后一题 if (corrent < count - 1) { corrent++; Question q = list.get(corrent); tv_title.setText(q.question); mRadioButton[0].setText(q.answerA); mRadioButton[1].setText(q.answerB); mRadioButton[2].setText(q.answerC); mRadioButton[3].setText(q.answerD); tv_result.setText(q.explaination); mRadioGroup.clearCheck(); // 设置选中 if (q.selectedAnswer != -1) { mRadioButton[q.selectedAnswer].setChecked(true); } } else if (corrent == count - 1 && wrongMode == true) { new AlertDialog.Builder(MainActivity.this) .setTitle("提示") .setMessage("已经到达最后一题,是否退出?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dialog, int which) { finish(); } }).setNegativeButton("取消", null).show(); } else { // 没有题目了,开始验证正确性 final List<Integer> wrongList = checkAnswer(list); if (wrongList.size() == 0) { new AlertDialog.Builder(MainActivity.this) .setTitle("提示") .setMessage("很棒哦") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dialog, int which) { finish(); } }).setNegativeButton("取消", null).show(); } // 窗口提示 new AlertDialog.Builder(MainActivity.this) .setTitle("恭喜,答题完成!") .setMessage( "恭喜你本次答题" + (list.size() - wrongList.size())*2 + "分" + "\n" + "答错了" + wrongList.size() + "道题" + "\n" + "是否查看错题?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dialog, int which) { wrongMode = true; List<Question> newList = new ArrayList<Question>(); for (int i = 0; i < wrongList .size(); i++) { newList.add(list.get(wrongList .get(i))); } list.clear(); for (int i = 0; i < newList.size(); i++) { list.add(newList.get(i)); } corrent = 0; count = list.size(); // 更新当前显示的内容 Question q = list.get(corrent); tv_title.setText(q.question); mRadioButton[0].setText(q.answerA); mRadioButton[1].setText(q.answerB); mRadioButton[2].setText(q.answerC); mRadioButton[3].setText(q.answerD); tv_result.setText(q.explaination); // 显示结果 tv_result .setVisibility(View.VISIBLE); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dialog, int which) { finish(); } }).show(); } } }); // 答案选中 mRadioGroup .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { for (int i = 0; i < 4; i++) { if (mRadioButton[i].isChecked() == true) { list.get(corrent).selectedAnswer = i; break; } } } }); } /* * 判断是否答题正确 */ private List<Integer> checkAnswer(List<Question> list) { List<Integer> wrongList = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { // 判断对错 if (list.get(i).answer != list.get(i).selectedAnswer) { wrongList.add(i); } } return wrongList; } /* * 将数据库拷贝到相应目录 */ private void initFile() { // 判断数据库是否拷贝到相应的目录下 if (new File(DB_PATH + DB_NAME).exists() == false) { File dir = new File(DB_PATH); if (!dir.exists()) { dir.mkdir(); } // 复制文件 try { InputStream is = getBaseContext().getAssets().open(DB_NAME); OutputStream os = new FileOutputStream(DB_PATH + DB_NAME); // 用来复制文件 byte[] buffer = new byte[1024]; // 保存已经复制的长度 int length; // 开始复制 while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } // 刷新 os.flush(); // 关闭 os.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } } } }

2019 Python开发者日-培训

2019 Python开发者日-培训

150讲轻松搞定Python网络爬虫

150讲轻松搞定Python网络爬虫

设计模式(JAVA语言实现)--20种设计模式附带源码

设计模式(JAVA语言实现)--20种设计模式附带源码

YOLOv3目标检测实战:训练自己的数据集

YOLOv3目标检测实战:训练自己的数据集

java后台+微信小程序 实现完整的点餐系统

java后台+微信小程序 实现完整的点餐系统

三个项目玩转深度学习(附1G源码)

三个项目玩转深度学习(附1G源码)

初级玩转Linux+Ubuntu(嵌入式开发基础课程)

初级玩转Linux+Ubuntu(嵌入式开发基础课程)

2019 AI开发者大会

2019 AI开发者大会

玩转Linux:常用命令实例指南

玩转Linux:常用命令实例指南

一学即懂的计算机视觉(第一季)

一学即懂的计算机视觉(第一季)

4小时玩转微信小程序——基础入门与微信支付实战

4小时玩转微信小程序——基础入门与微信支付实战

Git 实用技巧

Git 实用技巧

Python数据清洗实战入门

Python数据清洗实战入门

使用TensorFlow+keras快速构建图像分类模型

使用TensorFlow+keras快速构建图像分类模型

实用主义学Python(小白也容易上手的Python实用案例)

实用主义学Python(小白也容易上手的Python实用案例)

程序员的算法通关课:知己知彼(第一季)

程序员的算法通关课:知己知彼(第一季)

MySQL数据库从入门到实战应用

MySQL数据库从入门到实战应用

机器学习初学者必会的案例精讲

机器学习初学者必会的案例精讲

手把手实现Java图书管理系统(附源码)

手把手实现Java图书管理系统(附源码)

极简JAVA学习营第四期(报名以后加助教微信:eduxy-1)

极简JAVA学习营第四期(报名以后加助教微信:eduxy-1)

.net core快速开发框架

.net core快速开发框架

玩转Python-Python3基础入门

玩转Python-Python3基础入门

Python数据挖掘简易入门

Python数据挖掘简易入门

微信公众平台开发入门

微信公众平台开发入门

程序员的兼职技能课

程序员的兼职技能课

Windows版YOLOv4目标检测实战:训练自己的数据集

Windows版YOLOv4目标检测实战:训练自己的数据集

HoloLens2开发入门教程

HoloLens2开发入门教程

微信小程序开发实战

微信小程序开发实战

Java8零基础入门视频教程

Java8零基础入门视频教程

Python可以这样学(第一季:Python内功修炼)

Python可以这样学(第一季:Python内功修炼)

C++语言基础视频教程

C++语言基础视频教程

Python可以这样学(第四季:数据分析与科学计算可视化)

Python可以这样学(第四季:数据分析与科学计算可视化)

网络工程师小白入门--【思科CCNA、华为HCNA等网络工程师认证】

网络工程师小白入门--【思科CCNA、华为HCNA等网络工程师认证】

Python数据分析与挖掘

Python数据分析与挖掘

微信小程序开发实战之番茄时钟开发

微信小程序开发实战之番茄时钟开发

软件测试2小时入门

软件测试2小时入门

相关热词 c# 局部 截图 页面 c#实现简单的文件管理器 c# where c# 取文件夹路径 c# 对比 当天 c# fir 滤波器 c# 和站 队列 c# txt 去空格 c#移除其他类事件 c# 自动截屏
立即提问