安卓Studio笔记1,按钮和单选框,复选框

本文详细介绍了安卓 Studio 中按钮、图片按钮、单选按钮、复选框的基本使用,包括设置文字、监听器和事件触发。还涵盖了如何在 RadioGroup 和 CheckBox 中实现选中状态的判断与回调操作。
摘要由CSDN通过智能技术生成

安卓Studio笔记

按钮

  1. android:text=“我爱你” 设置显示的文字

    2.监听器

通过匿名内部类 要设置id
在这里插入图片描述
在这里插入图片描述

通过onClick

 public void MyClick(View v)
    {
        Toast.makeText(MainActivity.this,"单击了",Toast.LENGTH_SHORT).show();
    }

需要添加onClick
需要在Antivi_main.xml添加onClick并设置名字,和方法名一致
结果为这个

图片按钮

  • 和Button的相同之处,单击都可以触发onClick事件
  • 不同点:ImageButton没有android:text属性
    <ImageButton
        android:id="@+id/im1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/bgstart"
        android:background="#0000"
        />
        //background="#0000"设置背景为透明
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dsfihoihdgoifdhi"
        />
    <ImageButton
        android:id="@+id/im2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/adsdfd1"
        android:background="#0000"
        />
       getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
       //设置全屏显示
        ImageButton bt=(ImageButton) findViewById(R.id.im1);
        bt.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"单击了开始",Toast.LENGTH_LONG).show();
            }
        });
        ImageButton bt1=(ImageButton) findViewById(R.id.im2);
        bt1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"啥都没有",Toast.LENGTH_LONG).show();
            }
        });

单选按钮

<RadioButton
		android:id="@+id/rg1"//在获取值时添加
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="男" 
        android:checked="true"/>//默认选中

在一组单选按钮中只能有一个被选中,需要添加RadioGroup包裹RadioButton

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="男"/>
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="女"/>
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="保密"/>
   </RadioGroup>

在这里插入图片描述
在改变单选按钮的值时获取改变的值

        RadioGroup rg=(RadioGroup)findViewById(R.id.rg1);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton r=(RadioButton)findViewById(checkedId);
                r.getText();
                Toast.makeText(MainActivity.this,"性别"+r.getText(),Toast.LENGTH_SHORT).show();
            }
        });

在这里插入图片描述
添加按钮获取改变的
该方法的 RadioGroup rg;需要定义为全局变量

    <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="男"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="女"/>
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="保密"/>
   </RadioGroup>
    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确定"
        />
        Button b=(Button)findViewById(R.id.b1);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(int i=0;i<rg.getChildCount();i++)
                {
                    RadioButton r=(RadioButton)rg.getChildAt(i);
                    if(r.isChecked()){
                        Toast.makeText(MainActivity.this,"性别"+r.getText(),Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
            }
        });

判断是否选中想要的单选框

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是不是你亲爱的爸爸"
        android:textSize="19dp"
        android:textColor="#55E405E4"
        />
    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="是"/>
        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="不是"/>
        <RadioButton
            android:id="@+id/rb3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="不知道"/>
    </RadioGroup>
    <Button
        android:id="@+id/bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"
        />

在这里插入图片描述

复选框

    <CheckBox
        android:id="@+id/cb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="美术"/>
    <CheckBox
        android:id="@+id/cb2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="语文"
        android:checked="true"/>//默认为选中状态
    <CheckBox
        android:id="@+id/cb3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="英语"/>
    lick1=(CheckBox) findViewById(R.id.cb1);
    lick1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (lick1.isChecked()){//判断是否选中
                Toast.makeText(MainActivity.this, lick1.getText(),Toast.LENGTH_SHORT).show();
            }
        }
    });
Android Studio中,你可以使用XML布局文件来定义UI组件,如按钮、文本框、单选按钮、多选按钮等,并通过Java或Kotlin代码来处理用户的交互事件,例如登录和注册。以下是一个简单的介绍: 1. 按钮(Button): 你可以通过在XML布局文件中添加`<Button>`标签来创建一个按钮。然后,在Activity中通过`findViewById`找到这个按钮,并为其设置点击事件监听器。 ```xml <Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击我" /> ``` ```java Button myButton = findViewById(R.id.myButton); myButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理点击事件 } }); ``` 2. 文本框(EditText): 文本框用于用户输入文本,使用`<EditText>`标签创建。可以设置不同的输入类型,比如密码、文本等。 ```xml <EditText android:id="@+id/myEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> ``` 3. 单选按钮(RadioButton)和多选按钮(CheckBox): 单选按钮通常使用`<RadioGroup>`包裹多个`<RadioButton>`标签来实现互斥选择。多选按钮则直接使用`<CheckBox>`标签。 ```xml <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/radio_button_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项1" /> <RadioButton android:id="@+id/radio_button_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项2" /> </RadioGroup> ``` ```xml <CheckBox android:id="@+id/myCheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="多选框" /> ``` 4. 登录和注册功能:通常需要用户输入用户名和密码,可以使用`<EditText>`来获取输入,然后通过按钮触发登录或注册的事件处理函数。在处理函数中,你需要验证输入的正确性,并与服务器进行交互来完成认证。 ```java // 登录事件处理函数示例 public void onLoginClicked(View view) { EditText usernameEditText = findViewById(R.id.usernameEditText); EditText passwordEditText = findViewById(R.id.passwordEditText); String username = usernameEditText.getText().toString(); String password = passwordEditText.getText().toString(); // 验证用户名密码并登录 } ``` 实现登录和注册功能还需要考虑很多其他方面,比如输入验证、错误处理、网络请求等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值