Android——按钮类控件

【注】

1、弹出对话框:Dialog dialog=new AlertDialog.Builder(MainActivity.this).setTitle("提示").setMessage("这是一个图片按钮\n点击确定更换图片").setPositiveButton("确定",new DialogInterface.OnClickListener() 

                            {
                            @Override//这里写点击确定后执行的代码
                           
public voidonClick(DialogInterface dialogInterface, inti) {
                                imageButton.setImageDrawable(getDrawable(R.drawable.konan1));
                            }}).create();

2、弹出提示框: Toast.makeText(MainActivity.this,"你选中了红色",Toast.LENGTH_SHORT).show();
3、获取指定图片的drawable类型:getDrawable(R.drawable.picturename)

【Button类】

继承自TextView类,主要是设置View.OnClickListener监听器并实现相应功能。

该类控件包含普通下压按钮、单选按钮、复选按钮和分组框等。

【ImageButton类】

继承自ImageView类,与button类功能类似,无text属性,通过android:src和setImageResource(int)来设置图片。

【ToggleButton类】

双按钮,继承自CompoundButton(继承自button)。有checked、textOff、textOn属性,如设置“开灯/关灯”功能

【RadioButton类】

单选按钮,只有选中和未选中两种状态,同一时刻RadioGroup中智能有一个RadioButton被选中。

【CheckBox类】

可以多选,继承自CompoundButton。


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.button.MainActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="长按红"
            android:id="@+id/btnRed"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="点击蓝"
            android:id="@+id/btnBlue"
            android:layout_marginStart="22dp"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/btnRed"
            />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout"
        android:layout_alignParentStart="true"
        android:weightSum="1"
        android:id="@+id/linearLayout2">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageButton"
            android:src="@drawable/konan"
            />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout2"
        android:layout_alignParentStart="true"
        android:weightSum="1"
        android:id="@+id/linearLayout3">

        <ImageView
            android:layout_width="123dp"
            android:layout_height="124dp"
            android:id="@+id/imageView"
            android:src="@drawable/konan"
            />

        <ToggleButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New ToggleButton"
            android:id="@+id/toggleButton"
            android:textOn="显示"
            android:textOff="隐藏"
            />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout3"
        android:layout_alignParentStart="true"
        android:id="@+id/linearLayout4">

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:id="@+id/radiogroup"
            android:gravity="center">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Red"
                android:id="@+id/radioButton"
                android:checked="false"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Blue"
                android:id="@+id/radioButton2"
                android:checked="false"/>
        </RadioGroup>

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout4"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="false">

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New CheckBox"
            android:id="@+id/checkBox"
            />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New CheckBox"
            android:id="@+id/checkBox2"
            />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New CheckBox"
            android:id="@+id/checkBox3"
            />
    </LinearLayout>

</RelativeLayout>
 

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        //<editor-fold>普通button点击事件修改button背景色
        Button btnRed=(Button)findViewById(R.id.btnRed);
        final Button btnBlue=(Button)findViewById(R.id.btnBlue);
        //绑定事件源和监听对象
        btnBlue.setOnClickListener(new MyButtonListenser());
        btnRed.setOnLongClickListener(
                new View.OnLongClickListener()
                {
            @Override
            public boolean onLongClick(View view) {
                view.setBackgroundColor(Color.RED);
                return true;
            }
        });
        //</editor-fold>

        //<editor-fold>图片按钮
        final ImageButton imageButton=(ImageButton)findViewById(R.id.imageButton);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Dialog dialog=new AlertDialog.Builder(MainActivity.this).setTitle("提示")
                        .setMessage("这是一个图片按钮\n点击确定更换图片").setPositiveButton("确定", new
                                DialogInterface
                                .OnClickListener() {
                            @Override//这里写点击确定后执行的代码
                            public void onClick(DialogInterface dialogInterface, int i) {
                                imageButton.setImageDrawable(getDrawable(R.drawable.konan1));
                            }
                        }).create();//注意必须create才能创建
                dialog.show();
            }
        });
        //</editor-fold>

        //<editor-fold>双选按钮
        final ToggleButton toggleButton=(ToggleButton)findViewById(R.id.toggleButton);
        final ImageView imageView=(ImageView)findViewById(R.id.imageView);
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b)
                {
                    toggleButton.setChecked(true);
                    imageView.setImageDrawable(null);
                }
                else
                {
                    toggleButton.setChecked(false);
                    imageView.setImageDrawable(getDrawable(R.drawable.konan));
                }
            }
        });
        //</editor-fold>

        //<editor-fold>RdioButton
        RadioGroup rg=(RadioGroup)findViewById(R.id.radiogroup);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i)
                {
                    case R.id.radioButton:
                        Toast.makeText(MainActivity.this,"你选中了红色",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.radioButton2:
                        Toast.makeText(MainActivity.this,"你选中了蓝色",Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
        //</editor-fold>
    }

    class MyButtonListenser implements View.OnClickListener
    {
        public void onClick(View v)
        {
            v.setBackgroundColor(Color.BLUE);
        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个集成目前Android主流优秀第三方组件、优秀好用的自定义控件、实用工具封装、 以及一些APP共通模块(比如:版本更新、意见反馈、引导界面等等)的开发包,帮助程序员 快速开发自己的APP 已集成第三方开源组件: 网络请求库android-async-http 图片异步加载库universal-image-loader Sqlite数据库操作ormlite-android 社会化分享ShareSDK+短信验证码 Zxing二维码库 百度地图定位SDK 谷歌依赖注入库RoboGuice WebService调用库ksoap2 XML解析XStream 动画开源库nineoldandroids 表单验证库android-validation-komensky 更多优秀开源库等待集成... 已封装工具: HTTP网络通信工具(ToolHTTP.java),get/post请求,支持多种接口回调 SOAP协议通信工具(ToolSOAP.java),基于异步回调请求WebService接口 Sqlite数据库操作工具(ToolDatabase.java),获取DAO、创建表、删除表等API 提示信息对话框工具(ToolAlert.java),已集成泡泡、土司、对话框三种提示 文件操作工具(ToolFile.java),assets/raw/xml/shrePerface/等文件读写API 地图定位工具(ToolLocation.java),读取GPS状态、请求定位、获取经纬度等方法 社会化分享工具(ToolShareSDK.java),各大开发平台分享API操作 短信验证码工具(ToolMSM.java),移动/联通/电信三网发送手机短信验证码、异步回调验 证结果 字符串操作工具(ToolString.java),生成UUID、非空非NULL逻辑判断、生成MD5等常用共 通方法 数据操作工具(ToolData.java),自动递归获取表单数据封装成Map、本地数据分页共通方 法等 图片操作工具(ToolPicture.java),生成二维码、验证码、灰度、合成、圆角、水印等操 作 读取本地资源工具(ToolResource.java),反射本地资源文件API,避免依赖R文件,方便 jar形式发布 Android单位转换工具(ToolUnit.java),sp/dp/px互转 自定义Toast工具(ToolToast.java),自定义背景色、字体颜色、大小、边框等 Properties操作工具(ToolProperties.java),读写Properties文件操作 网络操作工具(ToolNetwork.java),获取网络信息、更改切换网络等相关操作 日期操作工具(ToolDateTime.java),获取日期、日期加减、格式化日期、日期转换等操作 XML操作工具(ToolXml.java),基于DOM/XMLPullPaser模式解析、生成XML操作 XMPP操作工具(ToolXMPP.java),基于XMPP协议的相关API操作 适配字体工具(ToolAutoFit.java),代码根据设备密度自动缩放View的字体大小 LOG相关工具(ToolLog.java) 功能待续-->切入记录异常日志,并存储文件或上传至服务 器 已封装/收集自定义控件: 兼容低版本的SwitchButton 追加自定义属性Value的CheckBox/RadioButton/RadioGroup/SingleSpinner 圆角提示信息TipsView 圆角图片RoundImageView 自定义样式风格Progres

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值