Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说CheckBox、RadioButton与EditText的使用,希望能够帮助你!!!。
CheckBox:复选框,有两种状态,选中和未选中,一般情况下是单独出现,表示给定标题的是与非,最常见的场景就是登录界面的用户协议;
RadioButton:单选按钮,也是有选中和未选中两种状态,但它是多个一起出现,最常见的是它与RadioGroup配套使用,用于多选一的情况;
EditText:编辑框,用于文本输入,使用场景很多,例如:登录界面的用户名和密码的输入、聊天界面的输入框、浏览器或者其他地方的搜索框等
1、打开之前创建好的项目,打开activity_main.xml文件,然后添加上述控件,如图1
图1
2、出于个人使用习惯,我把之前创建的按钮放在了最后面,为了方便截图,我把RadioGroup折叠起来了,其内容如下
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="选项一"/>
<RadioButton
android:id="@+id/rb02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选项二"/>
<RadioButton
android:id="@+id/rb03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选项三"/>
</RadioGroup>
1、打开MainActivity.java,先对代码做如下调整,即定义和绑定控件
public class MainActivity extends AppCompatActivity {
private TextView tvText;
private Button btnChange;
//2022-09-04添加代码
private CheckBox cbAgreement;
private RadioButton rbItem1;
private RadioButton rbItem2;
private EditText etContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setListener();
}
private void setListener() {
btnChange.setOnClickListener((v)->{
tvText.setText("我是点击后的文本");
});
}
private void initView() {
tvText=findViewById(R.id.tv_text);
btnChange=findViewById(R.id.btn_change);
//2022-09-04添加代码
cbAgreement=findViewById(R.id.cb_agreement);
rbItem1=findViewById(R.id.rb01);
rbItem2=findViewById(R.id.rb02);
etContent=findViewById(R.id.et_content);
}
}
2、调整按钮的点击事件,获取各个控件的取值
btnChange.setOnClickListener((v)->{
if (!cbAgreement.isChecked()){
Toast.makeText(this,"同意协议才能继续喔~",Toast.LENGTH_SHORT).show();
return;
}
String str=etContent.getText().toString();
String selectStr="选项三";
if (rbItem1.isChecked()){
selectStr="选项一";
}else if (rbItem2.isChecked()){
selectStr="选项二";
}
tvText.setText("您的选择是:"+selectStr+",您输入了以下内容:"+str);
});
因为EditText在日常使用还是蛮多的,这里贴出官网提供的其它方法
Public methods |
|
|
void |
extendSelection(int index) Convenience for Selection#extendSelection. |
|
CharSequence |
getAccessibilityClassName() Return the class name of this object to be used for accessibility purposes. |
|
boolean |
getFreezesText() Return whether this text view is including its entire text contents in frozen icicles. |
|
Editable |
getText() Return the text that TextView is displaying. |
|
void |
selectAll() Convenience for Selection#selectAll. |
|
void |
setEllipsize(TextUtils.TruncateAt ellipsis) Causes words in the text that are longer than the view's width to be ellipsized instead of broken in the middle. |
|
void |
setSelection(int start, int stop) Convenience for Selection#setSelection(Spannable, int, int). |
|
void |
setSelection(int index) Convenience for Selection#setSelection(Spannable, int). |
|
void |
setText(CharSequence text, TextView.BufferType type) Sets the text to be displayed and the TextView.BufferType. |
1、双斜线“//”表示单行注释,编译器不会执行,一般用于开发者对自己代码的补充说明或修改记录;
2、可以看到无论是单选按钮还是复选框,我都用到了“isChecked()”方法,它就是用于判断当前控件是否选中;
3、Toast的使用,Toast.makeText(上下文,文本内容,显示长短).show();
4、遇到"return"关键字,后面的代码不再执行
今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
上一篇
已是最后文章
下一篇
已是最新文章