In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the Android radio multiple selection button how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Android radio multiple button how to use the article will have a harvest, let's take a look at it.
Radio button
Radio button class: RadioButton
Android:checked= "true" setting is selected by default
Radio button controls are usually used in conjunction with RadioGroup.
RadioGroup is a subclass of LinearLayout and is used to combine multiple radio buttons into a group.
Only one radio button in the same button group can be selected.
Second, multiple selection button
The usage is basically the same as that of Button
The CheckBox object .isChecked () method can be used to determine whether the check button is selected.
Effect drawing (multiple selections are written in a project, using a page jump):
Project catalog:
Multi-select button, two forms
Code:
Activity_main.xml
MainActivity.java
Package com.example.radioandcheckdemo; import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button; public class MainActivity extends Activity implements OnClickListener {private Button button1; private Button button2; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main) Button1 = (Button) findViewById (R.id.button1); button2 = (Button) findViewById (R.id.button2); button1.setOnClickListener (this); button2.setOnClickListener (this);} @ Override public void onClick (View v) {Intent intent = new Intent () Switch (v.getId ()) {case R.id.button1: / / jump page intent.setClass (MainActivity.this, RadioActivity.class); startActivity (intent); break; case R.id.button2: / / jump page intent.setClass (MainActivity.this, CheckActivity.class); startActivity (intent) Default: break;}
Activity_radio.xml
RadioActivity.java
Package com.example.radioandcheckdemo; import android.app.Activity;import android.os.Bundle;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.Toast; public class RadioActivity extends Activity implements OnCheckedChangeListener {private RadioGroup group1; private RadioGroup group2; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_radio) Group1 = (RadioGroup) findViewById (R.id.group1); group2 = (RadioGroup) findViewById (R.id.group2); group1.setOnCheckedChangeListener (this); group2.setOnCheckedChangeListener (this) } @ Override public void onCheckedChanged (RadioGroup group, int checkedId) {/ / several ways to display values / / checkedId selects RadioButton's id / * switch (checkedId) {case R.id.radio1: Toast.makeText (this, Toast.LENGTH_LONG). Show (); break Case R.id.radio2: Toast.makeText (this, Toast.LENGTH_LONG). Show (); break; case R.id.radio3: Toast.makeText (this, "eat", Toast.LENGTH_LONG). Show (); break Case R.id.radio4: Toast.makeText (this, "not eating", Toast.LENGTH_LONG). Show (); break; default: break;} * / / find the clicked RadioButton / / RadioButton radio = (RadioButton) findViewById (checkedId) / / take out the value in RadioButton / / String str = radio.getText (). ToString (); / / pop-up box displays the selected value / / Toast.makeText (this, str, Toast.LENGTH_LONG). Show (); / / two sets of data are displayed at the same time / / data is taken out according to RadioGroup, and return-1 String str = ""; int buttonId = group1.getCheckedRadioButtonId () is not selected. If (buttonId! =-1) {RadioButton radio = (RadioButton) findViewById (buttonId); str = "your gender is" + radio.getText (). ToString ();} else {str = "you did not choose gender";} buttonId = group2.getCheckedRadioButtonId () If (buttonId! =-1) {RadioButton radio = (RadioButton) findViewById (buttonId); str + = "have you eaten yet?" + radio.getText () .toString ();} Toast.makeText (this, str, Toast.LENGTH_LONG) .show ();}}
Activity_check.xml
CheckActivity.java
Package com.example.radioandcheckdemo; import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.Toast; public class CheckActivity extends Activity {private CheckBox check1; private CheckBox check2; private CheckBox check3; private CheckBox check4; private Button button1 Private OnCheckedChangeListener listenter = new OnCheckedChangeListener () {@ Override public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {/ / check the checkbox CheckBox check = (CheckBox) buttonView; / / take out the current check value String str = check.getText () .toString () / / decide whether to check the status if (isChecked) {str = "you learned" + str;} else {str = "you didn't learn" + str;} Toast.makeText (CheckActivity.this, str, Toast.LENGTH_LONG). Show ();}} @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_check); check1 = (CheckBox) findViewById (R.id.check1); check2 = (CheckBox) findViewById (R.id.check2); check3 = (CheckBox) findViewById (R.id.check3); check4 = (CheckBox) findViewById (R.id.check4) Button1 = (Button) findViewById (R.id.button1); / / Multi-box click event / * check1.setOnCheckedChangeListener (listenter); check2.setOnCheckedChangeListener (listenter); check3.setOnCheckedChangeListener (listenter); check4.setOnCheckedChangeListener (listenter) * / / submit button click event button1.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {String str = "I learned it"; boolean f = false; if (check1.isChecked ()) {str + = check1.getText () + "," F = true;} if (check2.isChecked ()) {str + = check2.getText () + ","; f = true;} if (check3.isChecked ()) {str + = check3.getText () + "," F = true;} if (check4.isChecked ()) {str + = check4.getText () + ","; f = true;} if (f) {str = str.substring (0, str.length ()-1) } Toast.makeText (CheckActivity.this, str, Toast.LENGTH_LONG). Show ();}});}} this is the end of the article on "how to use the Android Radio multiple Button". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use the Android radio multi-select button". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.