当前位置:网站首页>Android Basics - RadioButton (radio button)
Android Basics - RadioButton (radio button)
2020-11-07 20:58:00 【ZHAO_JH】
==* Here we start to reference event handling , You need to read the post first 《android Basics - Event handling 》==
brief introduction
RadioButton Radio buttons are placed in RadioGroup Use in , Get layout page selection item data in the back end
RadioButton Object methods | paraphrase |
---|---|
getChildCount( ) | Get the number of radio buttons in the button group ; |
getChinldAt(i) | Get our radio button based on the index value |
isChecked( ) | Determine whether the button is selected |
getText() | Get value |
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Please choose gender "
android:textSize="23dp"
/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/btnMan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" male "
android:checked="true"/>
<RadioButton
android:id="@+id/btnWoman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Woman "/>
</RadioGroup>
<Button
android:id="@+id/btnpost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myclick"
android:text=" Submit "/>
</LinearLayout>
Listen to the button group ID Select trigger
MainActivity.java
package com.example.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// preservation Activity The state of
super.onCreate(savedInstanceState);
// Load view
setContentView(R.layout.activity_main);
// Get radio button group object
RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup);
// Listen button
radgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
// The radio button group object is passed in as a parameter , In order to get the radio object
public void onCheckedChanged(RadioGroup group, int checkedId) {
// Get radio objects
RadioButton radbtn = (RadioButton) findViewById(checkedId);
Toast.makeText(getApplicationContext(), " Button group value changes , You chose " + radbtn.getText(), Toast.LENGTH_LONG).show();
}
});
}
}
onClick Commit trigger
package com.example.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// preservation Activity The state of
super.onCreate(savedInstanceState);
// Load view
setContentView(R.layout.activity_main);
}
public void myclick(View source)
{
// Get radio button group object
RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup);
// Traverse the objects in the radio button group , Find a checked Object of property ( That is to find the selected object )
for (int i = 0; i < radgroup.getChildCount(); i++) {
RadioButton rd = (RadioButton) radgroup.getChildAt(i);
if (rd.isChecked()) {
Toast.makeText(getApplicationContext(), " Click Submit button , What you choose is :" + rd.getText(), Toast.LENGTH_LONG).show();
break;
}
}
}
}
版权声明
本文为[ZHAO_JH]所创,转载请带上原文链接,感谢
边栏推荐
- C++在C的基础上改进了哪些细节
- 尾-递
- Kylin on kubernetes' practice on eBay
- Big data algorithm - bloon filter
- 盘点那些争议最大的编程观点,你是什么看法呢?
- Reflection on a case of bus card being stolen and swiped
- Web安全(三)---CSRF攻击
- 什么都2020了,LINQ查询你还在用表达式树
- supervisor和Python多进程multiprocessing使用 子进程残留问题
- The most hard core of the whole network explains the computer startup process
猜你喜欢
On the coverage technology and best practice of go code
密码学-尚硅谷
Got timeout reading communication packets解决方法
Analysis of kubernetes service types: from concept to practice
构造请求日志分析系统
Let's talk about the locks in the database
云计算之路-出海记:整一台 aws 免费云服务器
编程界大佬教你:一行Python代码能做出哪些神奇的事情?
盘点那些争议最大的编程观点,你是什么看法呢?
三步一坑五步一雷,高速成长下的技术团队怎么带?
随机推荐
构造请求日志分析系统
static+代码块+多态+异常
[random talk] the goal and way of software design
Reflection on a case of bus card being stolen and swiped
快速上手Git
laravel8更新之维护模式改进
More than 50 object detection datasets from different industries
团灭 LeetCode 股票买卖问题
android基础-RadioButton(单选按钮)
websocket+probuf.原理篇
Thinkphp6中where条件中字段与字段比较条件的写法
Kylin on Kubernetes 在 eBay 的实践
Implementation of multi GPU distributed training with horovod in Amazon sagemaker pipeline mode
Kylin on kubernetes' practice on eBay
一万四千字分布式事务原理解析,全部掌握你还怕面试被问?
三步一坑五步一雷,高速成长下的技术团队怎么带?
Analysis of kubernetes service types: from concept to practice
Web安全(三)---CSRF攻击
Practice of Xiaoxiong school development board: real equipment access of smart street lamp sandbox experiment
Share several vs Code plug-ins I use everyday