当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Web安全(二)---跨域资源共享
- 虚拟DOM中给同一层级的元素设置固定且唯一的key为什么能提高性能
- 洞察——风格注意力网络(SANet)在任意风格迁移中的应用
- Web安全(三)---CSRF攻击
- 使用 Xunit.DependencyInjection 改造测试项目
- Annual salary of 900000 programmers is not as good as 3800 civil servants a month? How to choose between stability and high income?
- Share several vs Code plug-ins I use everyday
- 是时候结束 BERTology了
- Facebook开源框架如何简化 PyTorch 实验
- AFO记
猜你喜欢
Principles of websocket + probuf
构造请求日志分析系统
Annual salary of 900000 programmers is not as good as 3800 civil servants a month? How to choose between stability and high income?
Do not understand the underlying principle of database index? That's because you don't have a B tree in your heart
Summary of the resumption of a 618 promotion project
WPF 关于绘图个人总结
A detailed explanation of microservice architecture
Deep into web workers (1)
The most hard core of the whole network explains the computer startup process
浅谈HiZ-buffer
随机推荐
使用 Xunit.DependencyInjection 改造测试项目
Improvement of maintenance mode of laravel8 update
Reflection on a case of bus card being stolen and swiped
Principles of websocket + probuf
websocket+probuf.原理篇
What kind of technical ability should a programmer who has worked for 1-3 years? How to improve?
Do not understand the underlying principle of database index? That's because you don't have a B tree in your heart
Kubernetes服务类型浅析:从概念到实践
Dynamic programming -- state compression DP of set represented by binary
屏读时代,我们患上了注意力缺失候群症
Cpp(三) 什么是CMake
AFO
Deep into web workers (1)
Writing method of field and field comparison condition in where condition in thinkphpp6
低代码 vs 模型驱动,它们之间到底是什么关系?
不懂数据库索引的底层原理?那是因为你心里没点b树
android基础-RadioButton(单选按钮)
统计文本中字母的频次(不区分大小写)
Git代码提交操作,以及git push提示failed to push some refs'XXX'
awk实现类sql的join操作