本篇文章给大家谈谈如何在Android中验证单选按钮组?,以及如何在android中验证单选按钮组的功能的知识点,同时本文还将给你拓展android–单选按钮多行、android–如何从动态创建的单选
本篇文章给大家谈谈如何在Android中验证单选按钮组?,以及如何在android中验证单选按钮组的功能的知识点,同时本文还将给你拓展android – 单选按钮多行、android – 如何从动态创建的单选按钮中选择一个单选按钮?、android – 如何在radiogroup中将单选按钮设置为默认值?、android – 如何用单选按钮创建一个微调器等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:- 如何在Android中验证单选按钮组?(如何在android中验证单选按钮组的功能)
- android – 单选按钮多行
- android – 如何从动态创建的单选按钮中选择一个单选按钮?
- android – 如何在radiogroup中将单选按钮设置为默认值?
- android – 如何用单选按钮创建一个微调器
如何在Android中验证单选按钮组?(如何在android中验证单选按钮组的功能)
我在Android游戏应用中有一个radiobuttongroup.在用户继续进行实际游戏之前,他必须选择一个级别,即三个单选按钮.现在,如果用户单击“播放”,则应用程序崩溃.如何使用验证来查看是否选择了按钮?
我也有一个edittext,我只使用了一个:if((editText.getText().toString().equals(“”)))
看看用户是否写了名字,但是这在单选按钮上不起作用,或者即使我尝试使用这种检查,至少我的游戏也崩溃了.
任何帮助表示赞赏!
解决方法:
您可以获取包含3个单选按钮的RadioGroup id.
RadioGroup radiogrp = (RadioGroup) findViewById(R.id.RadioGroup);
int id = radiogrp.getCheckedRadioButtonId();
这将返回该组中所选单选按钮的标识符.空选择时,返回值为-1.
因此,您可以放置条件,并检查id == -1而不是未选中任何单选按钮.
android – 单选按钮多行
我想显示相同无线电组的6个单选按钮.但是所有6个单选按钮都保持在同一条线上并离开屏幕.如何以两行显示它们(每个3个单选按钮)?
我为我尝试了一切(我是android新手).
解决方法:
从搜索周围,似乎没有办法,因为RadioGroup使用LinearLayout,它不会包装.由于单选按钮必须是直接子节点到无线电组,因此无法将子布局添加到无线电组.
这意味着您必须手动实现此布局行为.两种可能的选择是:
>创建RadioGroup的副本以扩展不同的布局,或者至少允许您动态控制它.
>实现您自己的自定义布局以替换扩展您选择的布局的RadioGroup,并实现OnClickListener.有一个很好的例子here.
android – 如何从动态创建的单选按钮中选择一个单选按钮?
我有一个问题,如“最近的调查发现,在英国的中学”有4个选项,如
一个.
B.
C.
D.
所有数据都以json形式来自服务器.我用一个问题动态创建了视图,使用单选按钮创建了四个选项.但我所理解的是每一行都是新行,我无法从所有单选按钮中只选择一个单选按钮.每个按钮都单独选择,但我只想选择一个单击的单选按钮.
任何帮助,将不胜感激.
你可以在下面看到我的观点:
我的代码是:
LinearLayout linearLayoutForQuestions = (LinearLayout) view.findViewById(R.id.questionsLinearLayout); linearLayoutForQuestions.removeAllViews(); //set the questions for the user for (int ss = 0; ss < totalNoOfQuestions.size(); ss++) { final List<String> list = new ArrayList(); list.add("SELECT"); TextView textView = new TextView(activity); textView.setTextSize(15); textView.setTextColor(view.getResources().getColor(R.color.black)); textView.setTypeface(typeface1); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); params.setMargins(0,20,0); textView.setLayoutParams(params); String question = totalNoOfQuestions.get(ss).getQuestions(); String questionNo = totalNoOfQuestions.get(ss).QuestionNo; textView.setText("Question " + questionNo + " " + question); //linearlayout and set data inside it LinearLayout parent = new LinearLayout(activity); parent.removeAllViews(); parent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)); parent.setorientation(LinearLayout.VERTICAL); int optionSize = totalNoOfQuestions.get(ss).getoptions().size(); for (int s = 0; s < optionSize; s++) { //children of parent linearlayout LinearLayout layout2 = new LinearLayout(activity); layout2.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)); layout2.setorientation(LinearLayout.HORIZONTAL); layout2.setGravity(Gravity.CENTER); String optionNo = totalNoOfQuestions.get(ss).getoptions().get(s).getQuestionoptionNo(); TextView textView1 = new TextView(activity); textView1.setText(optionNo); textView.setTextSize(15); final RadioButton radioButton = new RadioButton(activity.getApplicationContext()); radioButton.setId(s); radioButton.setBackgroundResource(R.drawable.settings_background_ripple); LinearLayout.LayoutParams pa = new LinearLayout.LayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)); pa.setMargins(10,10,10); radioButton.setLayoutParams(pa); radioButton.setonCheckedchangelistener(new CompoundButton.OnCheckedchangelistener() { @Override public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { int checkedRadioButtonId = buttonView.getId(); Toast.makeText(activity,"hit it",Toast.LENGTH_SHORT).show(); } }); String questionoption = totalNoOfQuestions.get(ss).getoptions().get(s).getQuestionoption(); TextView textView2 = new TextView(activity); textView2.setTextSize(15); textView2.setText(questionoption); layout2.addView(textView1); layout2.addView(radioButton); layout2.addView(textView2); parent.addView(layout2); } linearLayoutForQuestions.addView(textView); linearLayoutForQuestions.addView(parent); }
解决方法
谢谢大家的帮助.快乐编码:)
LinearLayout linearLayoutForQuestions = (LinearLayout) view.findViewById(R.id.questionsLinearLayout); linearLayoutForQuestions.removeAllViews(); totalNoOfQuestions = readingTests[countReadingTest].getQuestion(); //set the questions for the user for (int ss = 0; ss < totalNoOfQuestions.size(); ss++) { groupNo = ss; final List<String> list = new ArrayList(); list.add("SELECT"); TextView textView = new TextView(activity); textView.setTextSize(15); textView.setTextColor(view.getResources().getColor(R.color.black)); textView.setTypeface(typeface1); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,0); textView.setLayoutParams(params); String question = totalNoOfQuestions.get(ss).getQuestions(); String questionNum = totalNoOfQuestions.get(ss).QuestionNo; textView.setText("Question " + questionNum + " " + question); //linearlayout and set data inside it LinearLayout parent = new LinearLayout(activity); parent.removeAllViews(); parent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)); parent.setorientation(LinearLayout.VERTICAL); final int optionSize = totalNoOfQuestions.get(ss).getoptions().size(); final RadioButton[] radioButton = new RadioButton[optionSize]; int size = totalNoOfQuestions.size(); final RadioGroup[] radioGroup = new RadioGroup[size]; //you can also create in xml radioGroup[ss] = new RadioGroup(activity); radioGroup[ss].setId(ss + 100); radioGroup[ss].setorientation(RadioGroup.VERTICAL)ø; //this textview is for the optionsNo like A,B,C,D final TextView[] textView1 = new TextView[optionSize]; LinearLayout layoutoptionsNo = new LinearLayout(activity); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT); layoutoptionsNo.setorientation(LinearLayout.VERTICAL); layoutoptionsNo.setWeightSum(optionSize); layoutoptionsNo.setGravity(Gravity.CENTER); layoutoptionsNo.setPadding(10,0); layoutoptionsNo.setLayoutParams(layoutParams); LinearLayout layoutoptions = new LinearLayout(activity); layoutoptions.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f)); layoutoptions.setorientation(LinearLayout.HORIZONTAL); layoutoptions.setWeightSum(4); layoutoptions.setGravity(Gravity.CENTER); //inner loop for the options for every single question for (int s = 0; s < optionSize; s++) { String optionNo = totalNoOfQuestions.get(ss).getoptions().get(s).getQuestionoptionNo(); textView1[s] = new TextView(activity); textView1[s].setText(optionNo); textView1[s].setTextSize(15); textView1[s].setGravity(Gravity.CENTER); LinearLayout.LayoutParams pa = new LinearLayout.LayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT)); pa.setMargins(10,10); pa.weight = 1; textView1[s].setLayoutParams(pa); String questionoption = totalNoOfQuestions.get(ss).getoptions().get(s).getQuestionoption(); radioButton[s] = new RadioButton(activity); radioButton[s].setId(s); radioButton[s].setText(questionoption); radioButton[s].setTextSize(15); radioButton[s].setPadding(2,2,2); radioButton[s].setBackgroundResource(R.drawable.settings_background_ripple); LinearLayout.LayoutParams pa2 = new LinearLayout.LayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,1.0f)); pa2.setMargins(10,10); pa2.weight = 1; radioGroup[ss].setLayoutParams(pa2); radioGroup[ss].addView(radioButton[s]); layoutoptionsNo.addView(textView1[s]); } radioGroup[ss].setonCheckedchangelistener(new RadioGroup.OnCheckedchangelistener() { @Override public void onCheckedChanged(RadioGroup group,int checkedId) { int originalId ; int id = group.getId();{ originalId = id-100; } questionNo = totalNoOfQuestions.get(originalId).getQuestionNo(); optionNo = totalNoOfQuestions.get(0).getoptions().get(checkedId).getQuestionoptionNo(); } }); layoutoptions.addView(layoutoptionsNo); layoutoptions.addView(radioGroup[ss]); parent.addView(layoutoptions); linearLayoutForQuestions.addView(textView); linearLayoutForQuestions.addView(parent); } boolean showOptionHint = totalNoOfQuestions.get(0).OptionList; LinearLayout linearLayoutForOptions = (LinearLayout) view.findViewById(R.id.optionsLinearLayout); linearLayoutForOptions.removeAllViews(); if (showOptionHint == true) { //dynamic creation of options under the quesiton layout List<ReadingTest.QuestionBean.OptionsBean> questionoptions = readingTests[countReadingTest].getQuestion().get(0).getoptions(); for (int ss = 0; ss < questionoptions.size(); ss++) { String options = questionoptions.get(ss).getQuestionoption(); String optionsNo = questionoptions.get(ss).getQuestionoptionNo(); TextView textView = new TextView(activity); textView.setTextSize(18); textView.setTypeface(typeface); textView.setTextColor(view.getResources().getColor(R.color.black)); textView.setText(optionsNo + ". " + options); linearLayoutForOptions.addView(textView); } }
android – 如何在radiogroup中将单选按钮设置为默认值?
我已经动态创建了RadioGroup和RadioButton,如下所示:
RadioGroup radioGroup = new RadioGroup(context);
RadioButton radioBtn1 = new RadioButton(context);
RadioButton radioBtn2 = new RadioButton(context);
RadioButton radioBtn3 = new RadioButton(context);
radioBtn1.setText("Less");
radioBtn2.setText("normal");
radioBtn3.setText("More");
radioBtn2.setChecked(true);
radioGroup.addView(radioBtn1);
radioGroup.addView(radioBtn2);
radioGroup.addView(radioBtn3);
这里步骤radioBtn2.setChecked(true);导致radioBtn2始终被检查.这意味着我无法通过检查其他两个单选按钮(radioBtn1,radioBtn3)取消选中radioBtn2.我想让RadioGroup一次只能检查一个单选按钮(现在它可以一次检查两个单选按钮).
我怎么解决这个问题?
解决方法:
你应该像这样检查radiogroup中的radiobutton:
radiogroup.check(IdOfYourButton)
当然,您首先必须为您的单选按钮设置ID
编辑:我忘了,radioButton.getId()也适用,拉克什
EDIT2:
android:checkedButton="@+id/my_radiobtn"
适用于radiogroup xml
android – 如何用单选按钮创建一个微调器
在main.xml中,我想要一个带有两个单选按钮的spinner1和一个带有3个复选框的spinner2.我不知道如何在Main.java中定义和创建这个微调器.
需要一些帮助.
main.xml中
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Spinner
android:id="@id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
spinner1 – 需要有单选按钮,spinner2需要有多个复选框
main.java
privare Spinner spiner1,spiner2;
public void OnCreate(BUndle SaveInstaceState)
{
super.OnCreate(savedInstanceState);
setContentView(R.layout.main)
spiner1=(Spinner)findViewById(R.id.spinner1);
spiner2=(Spinner)findViewById(R.id.spinner2);
//what to do from here?
}
解决方法:
在res / values /中创建strings.xml文件并添加以下内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="spinnerstr">Choose an item</string>
<string-array name="spinner_array">
<item>apple</item>
<item>orange</item>
<item>grapes</item>
</string-array>
在你的spinner.java中,添加以下内容:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromresource(
this, R.array.spinner_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
希望这会帮助你.
我们今天的关于如何在Android中验证单选按钮组?和如何在android中验证单选按钮组的功能的分享已经告一段落,感谢您的关注,如果您想了解更多关于android – 单选按钮多行、android – 如何从动态创建的单选按钮中选择一个单选按钮?、android – 如何在radiogroup中将单选按钮设置为默认值?、android – 如何用单选按钮创建一个微调器的相关信息,请在本站查询。
本文标签: