Android 常用控件(二)

本文详细介绍了Android开发中常用的UI控件,包括Button按钮的使用及事件处理、RadioButton与RadioGroup的选择功能、CheckBox的复选操作,以及Toast的快速消息提示和日期对话框的创建方法。

直接进入正题

1.Button按钮控件
Button控件也是使用过程中用的最多的控件之一,所以需要好好掌握。用户可以通过单击 Button 来触发一系列事件,然后为 Button 注册监听器,来实现 Button 的监听事件。

先来看button的常用属性:

<Button

//控件id
android:id = "@+id/xxx"  @+id/xxx表示新增控件命名为xxx

//宽度与高度
android:layout_width="wrap_content"  //wrap_content或者match_parent
android:layout_height="wrap_content"  //wrap_content或者match_parent

//按钮上显示的文字 
android:text="theButton" //两种方式,直接具体文本或者引用values下面的string.xml里面的元素@string/button

//按钮字体大小
android:textSize="24sp"  //以sp为单位

//字体颜色
android:textColor="#0000FF"  //RGB颜色

//字体格式
android:textStyle="normal"  //normal,bold,italic分别为正常,加粗以及斜体,默认为normal

//是否只在一行内显示全部内容
android:singleLine="true"  //true或者false,默认为false

关于button类别和事件处理
这里写图片描述

Button的点击事件有两种实现方式,其一是在Activity中为Button的点击事件注册一个监听器,还有是调用OnClick方法:
(监听器)

public class MainActivity extends Activity {
    private EditText edittext;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edittext=(EditText) findViewById(R.id.edit_text);
        button = (Button) findViewById(R.id.button);
        //为button按钮注册监听器,并通过匿名内部类实现
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            //点击Button会改变edittext的文字为"点击了Button"
            edittext.setText("点击了Button");
            }
        }); 
    }
}

(OnClick)
使用OnClick属性,直接定义方法名

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_up"
        android:onClick="login"
        />

JAVA-MainActivity实现该方法

public void login(View view){
        //代码块
        }

2.RadioButton与RadioGroup
RadioButton(单选按钮)在 Android 平台上也比较常用,比如一些选择项会用到单选按钮。它是一种单个圆形单选框双状态的按钮,可以选择或不选择。在 RadioButton 没有 被选中时,用户通过单击来选中它。但是,在选中后,无法通过单击取消选中。

RadioGroup 是单选组合框,用于 将 RadioButton 框起来。在多个 RadioButton被 RadioGroup 包含的情况下,同一时刻只可以选择一个 RadioButton,并用 setOnCheckedChangeListener 来对 RadioGroup 进行监听。

属性:

<RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:id="@+id/rgp_app3_sex"
        >

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:id="@+id/rb_app3_boy"
            />
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:id="@+id/rb_app3_gril"
            android:layout_centerHorizontal="true"

            />

    </RadioGroup>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="看看"
        android:onClick="look"
        android:layout_marginBottom="10dp"
        />

下面给出在Activity中用 setOnCheckedChangeListener 来对 RadioGroup 进行监听的代码, 注意RadioGroup中的RadioButton也都是需要声明和通过控件的id来得到代表控件的对象。

public class MainActivity extends AppCompatActivity {

    private RadioGroup rgp_app3_sex; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取单选框控件
        rgp_app3_sex = (RadioGroup) findViewById(R.id.rgp_app3_sex);

    }


 //单选
    public void look(View view){
        //获取单选组中被选中的单选框id
        int checked=rgp_app3_sex.getCheckedRadioButtonId();
        //根据单选框id获取单选框
        RadioButton rb_app3_boy = (RadioButton) findViewById(checked);
        String sex=rb_app3_boy.getText().toString();
        Toast.makeText(this, sex, Toast.LENGTH_SHORT).show();
    }

3.CheckBox复选
CheckBox(复选按钮),顾名思义是一种可以进行多选的按钮,默认以矩形表示。与 RadioButton 相同,它也有选中或者不选中双状态。我们可以先在布局文件中定义多选按钮, 然后对每一个多选按钮进行事件监听 setOnCheckedChangeListener,通过 isChecked 来判断 选项是否被选中,做出相应的事件响应。

这里写图片描述

CheckBox的简单使用:

<CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="吃"
        android:id="@+id/cb_app3_eat"
        />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="睡"
        android:id="@+id/cb_app3_sleep"
        />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="玩"
        android:id="@+id/cb_app3_play"
        />
public class MainActivity extends AppCompatActivity {


    private CheckBox cb_app3_eat;
    private CheckBox cb_app3_sleep;
    private CheckBox cb_app3_play;
    List<CheckBox> lc=new ArrayList<CheckBox>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取复选框控件
        cb_app3_eat = (CheckBox) findViewById(R.id.cb_app3_eat);
        cb_app3_sleep = (CheckBox) findViewById(R.id.cb_app3_sleep);
        cb_app3_play = (CheckBox) findViewById(R.id.cb_app3_play);
        lc.add(cb_app3_eat);
        lc.add(cb_app3_sleep);
        lc.add(cb_app3_play);
    }


//多选
    public void cnm(View view){

        String hh="";
        for(CheckBox c:lc){
            if(c.isChecked()){
                hh+=c.getText().toString();
            }
        }

        if(hh==""){
            hh="未选中";
        }
        Toast.makeText(this, hh, Toast.LENGTH_SHORT).show();

    }

4.Toast (吐司)
Toast是Android提供的“快显讯息”类,它的用途很多,使用起来不难。

(1)public int getDuration ()

返回存续期间

 请参阅setDuration(int)



(2)public int getGravity ()

 取得提示信息在屏幕上显示的位置。      

 请参阅  Gravity setGravity()


(3)public static Toast makeText (Context context, int resId, int duration)

生成一个从资源中取得的包含文本视图的标准 Toast 对象。

参数:

  context 使用的上下文。通常是你的 Application 或 Activity 对象。

  resId  要使用的字符串资源ID,可以是已格式化文本。       

  duration 该信息的存续期间。值为 LENGTH_SHORT 或 LENGTH_LONG    




(4)public static Toast makeText (Context context, CharSequence text, int duration)

生成一个包含文本视图的标准 Toast 对象。

参数:

context

使用的上下文。通常是你的 Application 或 Activity 对象。

resId

要显示的文本,可以是已格式化文本。

duration

该信息的存续期间。值为 LENGTH_SHORT 或 LENGTH_LONG





(5)public void setDuration (int duration)

设置存续期间。

请参阅

LENGTH_SHORT

LENGTH_LONG



(6)public void setGravity (int gravity, int xOffset, int yOffset)

设置提示信息在屏幕上的显示位置。

(自定义Toast的显示位置,toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0)可以把Toast定位在左上角。Toast提示的位置xOffset:大于0向右移,小于0向左移

请参阅

Gravity

getGravity()




 (7)public void setText (CharSequence s)

更新之前通过 makeText() 方法生成的 Toast 对象的文本内容。

参数s   为 Toast 指定的新的文本



(8)public void setView (View view)

设置要显示的 View 。

(注意这个方法可以显示自定义的toast视图,可以包含图像,文字等等。是比较常用的方法。)

请参阅

getView()



(9)public void show ()

按照指定的存续期间显示提示信息。

简单使用实例:

public void login(View view){
        //获取用户名
        String uname=et_app3_uname.getText().toString();
        String upwd=et_app3_upwd.getText().toString();

            //弹出Toast
            Toast toast = Toast.makeText(this, "six six six:" + uname, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            //加图标
            LinearLayout ll = (LinearLayout) toast.getView();
            ll.setOrientation(LinearLayout.HORIZONTAL);
            ImageView toastimage = new ImageView(getApplicationContext());
            toastimage.setImageResource(R.drawable.jinx_r);
            ll.addView(toastimage, 0);
            toast.show();
        }

5.日期对话框Toast
适用于许多Android时间应用
属性:
这里写图片描述


简单应用:
//日期对话框
    public void getDate(View view){
        Calendar c=Calendar.getInstance();
        int year=c.get(Calendar.YEAR);
        int month=c.get(Calendar.MONTH);
        int day=c.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog dpd=new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int a, int b, int c) {
                Toast.makeText(MainActivity.this, a+"年"+(b+1)+"月"+c+"日", Toast.LENGTH_SHORT).show();
            }
        },year,month,day){};
        dpd.show();
    }

效果图:
这里写图片描述

该数据集通过合成方式模拟了多种发动机在运行过程中的传感器监测数据,旨在构建一个用于机械系统故障检测的基准资源,特别适用于汽车领域的诊断分析。数据按固定时间间隔采集,涵盖了发动机性能指标、异常状态以及工作模式等多维度信息。 时间戳:数据类型为日期时间,记录了每个数据点的采集时刻。序列起始于2024年12月24日10:00,并以5分钟为间隔持续生成,体现了对发动机运行状态的连续监测。 温度(摄氏度):以浮点数形式记录发动机的温度读数。其数值范围通常处于60至120摄氏度之间,反映了发动机在常规工况下的典型温度区间。 转速(转/分钟):以浮点数表示发动机曲轴的旋转速度。该参数在1000至4000转/分钟的范围内随机生成,符合多数发动机在正常运转时的转速特征。 燃油效率(公里/升):浮点型变量,用于衡量发动机的燃料利用效能,即每升燃料所能支持的行驶里程。其取值范围设定在15至30公里/升之间。 振动_X、振动_Y、振动_Z:这三个浮点数列分别记录了发动机在三维空间坐标系中各轴向的振动强度。测量值标准化至0到1的标度,较高的数值通常暗示存在异常振动,可能与潜在的机械故障相关。 扭矩(牛·米):以浮点数表征发动机输出的旋转力矩,数值区间为50至200牛·米,体现了发动机的负载能力。 功率输出(千瓦):浮点型变量,描述发动机单位时间内做功的速率,取值范围为20至100千瓦。 故障状态:整型分类变量,用于标识发动机的异常程度,共分为四个等级:0代表正常状态,1表示轻微故障,2对应中等故障,3指示严重故障。该列作为分类任务的目标变量,支持基于传感器数据预测故障等级。 运行模式:字符串类型变量,描述发动机当前的工作状态,主要包括:怠速(发动机运转但无负载)、巡航(发动机在常规负载下平稳运行)、重载(发动机承受高负荷或高压工况)。 数据集整体包含1000条记录,每条记录对应特定时刻的发动机性能快照。其中故障状态涵盖从正常到严重故障的四级分类,有助于训练模型实现故障预测与诊断。所有数据均为合成生成,旨在模拟真实的发动机性能变化与典型故障场景,所包含的温度、转速、燃油效率、振动、扭矩及功率输出等关键传感指标,均为影响发动机故障判定的重要因素。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值