[android]常见UI组件

一、常见UI组件

在这里插入图片描述

二、详细介绍组件属性及应用

(1)单选按钮- - -RedioButton

通常情况下,RedioButton组件需要与RedioGroup组件一起使用,组成一个单选按钮组。

  • 在改变单选按钮组的值时获取
<LinearLayout
        android:id="@+id/sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="50sp"
        android:layout_below="@id/ratingbar"
        android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="性别:"
        android:textColor="@color/black"
        android:textSize="18sp"/>
    <RadioGroup
        android:id="@+id/siglecheck"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:id="@+id/radio1"
            android:text=""
            android:checked="true"/>
        <RadioButton
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:id="@+id/radio2"
            android:text=""/>
    </RadioGroup>
</LinearLayout>
RadioGroup sex=(RadioGroup)findViewById(R.id.siglecheck) ;
sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
     @Override
     public void onCheckedChanged(RadioGroup group, int checkedId)     {
        RadioButton r = (RadioButton) findViewById(checkedId);
       
        AlertDialog.Builder builder=new AlertDialog.Builder(RatingbarActivity.this);
        //r.getText()获取被选中的单选的按钮的值;
        builder.setMessage("您的性别是:"+r.getText());
        builder.setPositiveButton("确定",null).show();
                
   }

在这里插入图片描述
(2)复选框- - -CheckBox
xml文件

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:text="你未来想要选择的职业是:"
        android:textColor="@color/black"
        android:textSize="18sp"/>

      <CheckBox
       android:id="@+id/radio1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:checked="true"
       android:text="c++工程师" />

      <CheckBox
          android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/radio2"
            android:text="算法工程师"/>
<CheckBox
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:id="@+id/radio3"
    android:text="嵌入式工程师"/>

<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="提交"/>

java代码

ublic class CheckBoxActivity extends AppCompatActivity {
    Button btn;
    CheckBox checkbox1,checkbox2,checkbox3;
    protected  void onCreate(Bundle savedIntanceState){
        super.onCreate(savedIntanceState);
        setContentView(R.layout.activity_main);

        btn=(Button) findViewById(R.id.btn);
        checkbox1=(CheckBox) findViewById(R.id.radio1);
        checkbox2=(CheckBox) findViewById(R.id.radio2);
        checkbox3=(CheckBox) findViewById(R.id.radio3);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String checked="";
                String spolit="  ";
                if(checkbox1.isChecked()){
                    checked+=checkbox1.getText().toString()+spolit;
                }
                if(checkbox2.isChecked()){
                    checked+=checkbox2.getText().toString()+spolit;
                }
                if(checkbox3.isChecked()){
                    checked+=checkbox3.getText().toString()+spolit;
                }
                //显示被选中的复选框对应的信息
                Toast.makeText(CheckBoxActivity.this,checked,Toast.LENGTH_LONG).show();
            }
        });
    }

}

在这里插入图片描述
(3).普通按钮–Button

  • 更改按钮颜色和样式
    在drawable目录中新建Drawable Resource File文件
    在这里插入图片描述
    修改样式
    在这里插入图片描述

Button背景无法修改,一直呈现亮紫色:
原因:由于新版本的主题问题导致
方法:①打开app/res/values/themes/themes.xml ②修改style,如下在这里插入图片描述

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android开发中,UI组件是构建用户界面的基本元素。以下是几个常用的UI组件及其基本使用方法: 1. TextView:用于显示文本内容。 ```java TextView textView = findViewById(R.id.textView); textView.setText("Hello, World!"); ``` 2. EditText:用于接收用户输入的文本。 ```java EditText editText = findViewById(R.id.editText); String userInput = editText.getText().toString(); ``` 3. Button:用于触发事件响应。 ```java Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理点击事件 } }); ``` 4. ImageView:用于显示图像。 ```java ImageView imageView = findViewById(R.id.imageView); imageView.setImageResource(R.drawable.my_image); ``` 5. ListView:用于显示列表数据。 ```java ListView listView = findViewById(R.id.listView); ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, data); listView.setAdapter(adapter); ``` 6. RecyclerView:用于高度灵活的列表数据展示。 ```java RecyclerView recyclerView = findViewById(R.id.recyclerView); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); RecyclerView.Adapter adapter = new MyAdapter(data); recyclerView.setAdapter(adapter); ``` 以上是一些常见UI组件及其基本使用方法。根据实际需求,你还可以探索更多丰富的UI组件和它们的使用方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值