Android开发笔记(三)组件

Text文本框

EditText编辑框 文本框的子类

常用属性

hint:提示信息 inputType:输入格式 textPassword number

lines:编辑行数 (确定之后超过换行)

 

通过编辑框对象.getText获取文本

ImageView

scaleType: fitXY 填充XY

button 同样是Text子类

 

  • ImageButton

无text属性   文字通过图片制作

背景设置透明:android:background = "#0000"


RadioButton

通过设置单选按钮组RadioGroup来实现单选

    <RadioGroup
        android:id="@+id/rg1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            />
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2"
            />
    </RadioGroup>
  • 获取选择值

 方法一 切换选项获取(MainActivity):

        RadioGroup rg1 = findViewById(R.id.rg1);
        rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton r = findViewById(checkedId);
                r.getText(); // 返回选择的text
            }
        });
方法二 用按钮获取:
加入到onClick()的执行代码中
        for(int i = 0; i < rg1.getChildCount(); i++) {
            RadioButton r = (RadioButton) rg1.getChildAt(i);
            if(r.isChecked()){
                Toast.makeText(MainActivity.this,"success" + r.getText(),Toast.LENGTH_SHORT).show();
                break;
            }
        }
getChildCount():获取在单选按钮组中单选按钮的个数
isChecked():选中返回ture

 

 

posted @ 2021-09-03 18:04  zxcl  阅读(46)  评论(0)    收藏  举报