In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the Android how to achieve the radio button related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this Android how to achieve the radio button article will have a harvest, let's take a look at it.
Radio button
By default, the radio button is displayed as a circular icon, and you can place some description text next to it. Typically, the RadioButton component needs to be used with the RadioGroup component to form a radio button group. A RadioGroup is a container that can hold multiple RadioButton.
The layout effect shows:
The android:checked property of the RadioButton component is used to specify the selected state. When android:checked= "true", it is selected; when android:checked= "false", it is unchecked.
There are three ways to get the selected value:
The first is to set up an event listener setOnCheckChangeListener for RadioButton.
Public class MainActivity extends AppCompatActivity {RadioGroup radioGroup; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); / / initialize control initView (); / / Click event clickEvent ();} private void initView () {radioGroup = findViewById (R.id.radioGroup) } private void clickEvent () {/ / bind monitor radioGroup.setOnCheckedChangeListener (new MyRadioButtonListener ()) to RadioGroup;} / / radio button listens to private class MyRadioButtonListener implements RadioGroup.OnCheckedChangeListener {@ Override public void onCheckedChanged (RadioGroup group, int checkedId) {RadioButton r = (RadioButton) findViewById (checkedId) / / get the selected Id Log.i ("Radio Button listening", "Select gender:" + r.getText () .toString ());}
Radio button listens to the log effect:
The second gets the value of the selected radio button by clicking another button.
Public class MainActivity extends AppCompatActivity implements View.OnClickListener {RadioGroup radioGroup; / / submit Button bt_submit; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); / / initialize control initView (); / / Click event clickEvent ();} private void initView () {radioGroup = findViewById (R.id.radioGroup) Bt_submit = findViewById (R.id.bt_submit);} private void clickEvent () {/ / submit bt_submit.setOnClickListener (this);} @ Override public void onClick (View v) {switch (v.getId ()) {case R.id.bt_submit: for (int I = 0; I < radioGroup.getChildCount () RadioButton +) {RadioButton r = (RadioButton) radioGroup.getChildAt (I); if (r.isChecked ()) {Log.i ("get when clicking other buttons", "Select gender:" + r.getText ());}} break;}}
Determine the log effect display of clicking another button to get the value of the selected radio button:
The third method determines which radio button's id is the id being clicked, and gets the value through id.
Public class MainActivity extends AppCompatActivity {RadioGroup radioGroup; / / male RadioButton radio_man; / / female RadioButton radio_female; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); / / initialize control initView (); / / Click event clickEvent () } private void initView () {radioGroup = findViewById (R.id.radioGroup); radio_man = findViewById (R.id.radio_man); radio_female = findViewById (R.id.radio_female);} private void clickEvent () {/ / bind monitor radioGroup.setOnCheckedChangeListener (new MyRadioButtonListener () to RadioGroup) } / / Radio button listens to private class MyRadioButtonListener implements RadioGroup.OnCheckedChangeListener {@ Override public void onCheckedChanged (RadioGroup group) Int checkedId) {/ / switch (checkedId) {case R.id.radio_female: / / Log.i when the user selects a woman ("judging to click on the radio button of Id", "select gender:" + radio_female.getText (). ToString ()) Break; case R.id.radio_man: / / when the user selects a male, Log.i ("judge to click the radio button of Id", "select gender:" + radio_man.getText (). ToString ()); break;}
Determine the log effect display of the radio button clicked:
This is the end of the article on "how to implement radio buttons in Android". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to achieve radio buttons in Android". 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.