点击事件 单选框 RadioButton 复选框 CheckBox ScrollView 滚动视图控件

Day 2

ImageView

插入图片

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/a"
    android:alpha="0.55"
    android:scaleType="fitStart"/>
android:alpha="0.55" //表示透明度范围 0~1 
 android:scaleType="fitStart"	//表面图片的对应位置 默认居中

示意图:

在这里插入图片描述

点击事件

一个按钮显示多个背景图 在不同的操作下显示不同的背景图

页面代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="15dp"
    android:gravity="center">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/textView4"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="手机号:"/>

        <EditText
            android:id="@+id/et_phonenum"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:gravity="center"
            android:hint="请输入手机号"
            android:inputType="number"/>
    </LinearLayout>
    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/textView5"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="密码:"/>

        <EditText
            android:id="@+id/et_pwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:gravity="center"
            android:hint="请输入密码"
            android:inputType="number"/>
    </LinearLayout>
    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/textView6"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="重置密码:"/>

        <EditText
            android:id="@+id/et_rewd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:gravity="center"
            android:hint="请再次输入密码"
            android:inputType="textPassword"/>
    </LinearLayout>
    <LinearLayout
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/a7"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="性别"/>
        <RadioGroup
            android:id="@+id/rg_sex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <RadioButton
                android:id="@+id/rb_man"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="男"/>
            <RadioButton
                android:id="@+id/rd_woman"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="女"
                android:layout_marginLeft="30dp"/>
        </RadioGroup>
    </LinearLayout>
    <CheckBox
        android:id="@+id/cd_adrult"
        android:layout_marginTop="150dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="已满18岁"/>
    <CheckBox
        android:id="@+id/cd_agree"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我同意相关条款"/>

    <Button
        android:id="@+id/tb_zhuce"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="注册" />

</LinearLayout>

效果图
在这里插入图片描述

单选按钮

单选框 RadioButton

单选框的独立存在: 主要用在同意协议等

单选框一般情况下都是多个出现:比如性别,必须跟RadioGroup结合使用
内容两个或两个以上 实际却只能选一个按钮的方法

 <RadioGroup
        ......>
            <RadioButton
            ......
            ....../>
            <RadioButton
            ........
             ......./>
             ...
        </RadioGroup>
// 单选框的点击事件是设置在RadioGroup上

onCheckedChangeListener() 

复选框 CheckBox

复选框的独立使用:主要用于 同意协议

复选框的多个使用:多选

复选框的点击事件:
可用于一下条款的按钮 可以点击也可以取消

 <CheckBox> <CheckBox/>
// 复选框的点击事件是设置在checkbox上

onCheckedChangeListener() 

StateListDrawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    //true 设置触发时的颜色
    <item android:drawable="@color/black" android:state_pressed="true"></item> 
    //false 没有触发操作的颜色
    <item android:drawable="@color/white" android:state_pressed="false"></item>
</selector>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"
        android:background="@drawable/sirwzbj"
        />

用户提示

使用内部类进行处理

    //使用内部类进行处理

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //1.获取页面上的控件
        //如何获取页面上的按钮 findViewById()
        //R.id.tb_zhuce 表示页面上的案件id
        Button tb_zhuce=findViewById(R.id.tb_zhuce);

        //2.给页面上的控件 设置监听
        //3.处理监听,一旦发现有人点击按钮 进行操作
        tb_zhuce.setOnClickListener(tb_zhuces);


    }
    //用内部类来 进行操作
    private View.OnClickListener tb_zhuces =new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //后台提示
            Log.i("tb_zhuce点击事件","新用户注册成功");
            //前台提示
            //Toast.makeText(参数1,参数2,参数3)
            //1:上下文 2:要给用户提示内容(要是字符串格式(如过是数字:123+""))
            Toast.makeText(MainActivity2.this, "功能尚未完成", Toast.LENGTH_SHORT).show();
        }
    };
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //1.获取页面上的控件
        //如何获取页面上的按钮 findViewById()
        //R.id.tb_zhuce 表示页面上的案件id
        Button tb_zhuce=findViewById(R.id.tb_zhuce);
        //2.给页面上的控件 设置监听
        //3.处理监听,一旦发现有人点击按钮 进行操作
        tb_zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("tb_zhuce点击事件","新用户注册成功");
                Toast.makeText(MainActivity2.this, "功能尚未完成", Toast.LENGTH_LONG).show();
            }
        });
    }
   @Override
    protected void onCreate(Bundle savedInstanceState) implements View.OnClickListener{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //1.获取页面上的控件
        Button tb_zhuce=findViewById(R.id.tb_zhuce);
        Button tb_zhuce1=findViewById(R.id.tb_zhuce1);
        Button tb_zhuce2=findViewById(R.id.tb_zhuce2);
        //2.给页面上的控件 设置监听
        //3.处理监听,一旦发现有人点击按钮 进行操作
        tb_zhuce.setOnClickListener(new View.OnClickListener(this);
        tb_zhuce.setOnClickListener(new View.OnClickListener(this);
        tb_zhuce.setOnClickListener(new View.OnClickListener(this);
        }
        
        
    /**
     * 用接口 View.OnClickListener及实现方法的方式来
     * 来处理多个按钮的操作
     * @param view
     */
     
     
    @Override
    public void onClick(View view) {
        if (view.getId()==R.id.tb_zhuce){
            Toast.makeText(MainActivity2.this, "按钮被点击", Toast.LENGTH_LONG).show();
        }else if ((view.getId()==R.id.tb_zhuce1){
            Toast.makeText(MainActivity2.this, "按钮1被点击", Toast.LENGTH_LONG).show();
        }else if ((view.getId()==R.id.tb_zhuce2){
            Toast.makeText(MainActivity2.this, "按钮2被点击", Toast.L
            ENGTH_LONG).show();
        }

点击的效果图如下:

在这里插入图片描述

ScrollView 滚动视图控件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity5"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/btn_top"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="回到顶部"/>
        <Button
            android:id="@+id/btn_bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="跳转底部"/>
    </LinearLayout>
    <ScrollView
        android:id="@+id/sv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="顶部" />
                
                .....
                
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="底部" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

效果图
在这里插入图片描述

public class MainActivity5 extends AppCompatActivity implements View.OnClickListener{
    //1.声明容量
    private ScrollView scrollView;
    private View btn_top;
    private View btn_bottom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //2.根据ID获取页面上的控件,并把控件赋值给容器
        setContentView(R.layout.activity_main5);
         btn_top = findViewById(R.id.btn_top);
         btn_bottom = findViewById(R.id.btn_bottom);
        scrollView =findViewById(R.id.sv);
         //3.设置监听
        btn_top.setOnClickListener(this);
        btn_bottom.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        if (view.getId()==R.id.btn_top){
            //返回顶部
            scrollView.fullScroll(ScrollView.FOCUS_UP);
        }else if (view.getId()==R.id.btn_bottom){
            //跳转底部
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
        }
    }
}

** 使用时的注意点?**

  1. 不用再ScrollView内部使用自带滚动条的控件或布局
  2. ScrollView直接子View只能有一个
  3. ScrollView只支持竖直滑动 如果想要横向滑动 HorizontalScrollView
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来给你提供一个简单的Java Swing代码示例,演示如何创建单选框复选框、标签和选择框的结构: ```java import javax.swing.*; import java.awt.*; public class DemoGUI extends JFrame { public DemoGUI() { // 创建一个面板 JPanel panel = new JPanel(); panel.setLayout(new GridLayout(4, 1)); // 设置面板布局 // 创建单选框 JRadioButton radioButton1 = new JRadioButton("选项1"); JRadioButton radioButton2 = new JRadioButton("选项2"); JRadioButton radioButton3 = new JRadioButton("选项3"); ButtonGroup group = new ButtonGroup(); // 创建按钮组 group.add(radioButton1); // 将单选框添加到按钮组中 group.add(radioButton2); group.add(radioButton3); panel.add(radioButton1); // 添加单选框到面板中 panel.add(radioButton2); panel.add(radioButton3); // 创建复选框 JCheckBox checkBox1 = new JCheckBox("选项1"); JCheckBox checkBox2 = new JCheckBox("选项2"); JCheckBox checkBox3 = new JCheckBox("选项3"); panel.add(checkBox1); // 添加复选框到面板中 panel.add(checkBox2); panel.add(checkBox3); // 创建标签 JLabel label = new JLabel("这是一个标签"); panel.add(label); // 添加标签到面板中 // 创建选择框 JComboBox<String> comboBox = new JComboBox<>(); comboBox.addItem("选项1"); comboBox.addItem("选项2"); comboBox.addItem("选项3"); panel.add(comboBox); // 添加选择框到面板中 // 将面板添加到窗口中 getContentPane().add(panel); setSize(400, 300); // 设置窗口大小 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗口关闭方式 setVisible(true); // 显示窗口 } public static void main(String[] args) { new DemoGUI(); // 启动窗口 } } ``` 此示例创建了一个包含单选框复选框、标签和选择框的简单GUI界面,并将它们添加到一个面板中。你可以根据自己的需要修改代码,以创建更复杂的GUI界面。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值