码迷,mamicode.com
首页 > 移动开发 > 详细

android基本控件学习-----Button

时间:2016-01-03 15:14:24      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

Button讲解:

一、在我们实际的使用button的时候经常会对button不同状态会有不同的显示,在讲解Button前,首先对drawable下面的statelistdrawable的相关知识讲一下,StateListDrawable在一中drawable下面的一种资源文件,它的关键节点selector,我只需要在设置button属性background的时候@drawable/selector_name就可以了,这时就会根据不同状态现在button的变化,当然这样StateListDrawable也适合其他一些控件,主要还是用于Button。

StateListDrawable我们可能用到的属性:

  • drawable:引用的Drawable位图,我们可以把他放到最前面,就表示组件的正常状态~
  • state_focused:是否获得焦点
  • state_window_focused:是否获得窗口焦点
  • state_enabled:控件是否可用
  • state_checkable:控件可否被勾选,eg:checkbox
  • state_checked:控件是否被勾选
  • state_selected:控件是否被选择,针对有滚轮的情况
  • state_pressed:控件是否被按下
  • state_active:控件是否处于活动状态,eg:slidingTab
  • state_single:控件包含多个子控件时,确定是否只显示一个子控件
  • state_first:控件包含多个子控件时,确定第一个子控件是否处于显示状态
  • state_middle:控件包含多个子控件时,确定中间一个子控件是否处于显示状态
  • state_last:控件包含多个子控件时,确定最后一个子控件是否处于显示状态

二:实例一:实现按钮按下效果和按钮的圆角

Java文件

package com.example.test3;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {
    private Button btn1;
    private Button btn2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.btn_one);
        btn2 = (Button) findViewById(R.id.btn_two);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (btn2.getText().toString().endsWith("按钮不可用")){
                    btn1.setEnabled(false);
                    btn2.setText("按钮可用");
                }else{
                    btn1.setEnabled(true);
                    btn2.setText("按钮不可用");
                }
            }
        });
    }
}

StateListDrawable文件:其中关于shape的使用前面已经讲过

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--按下按钮-->
    <item android:state_pressed="true">
        <shape>
            <stroke android:width="5px" android:color="@android:color/holo_red_light"/>
            <corners android:radius="15dp"/>
            <solid android:color="@android:color/holo_red_light"/>
        </shape>
    </item>
    <!-- 按钮不可用-->
    <item android:state_enabled="false">
        <shape>
            <stroke android:width="5px" android:color="@android:color/darker_gray"/>
            <corners android:radius="15dp"/>
            <solid android:color="@android:color/darker_gray"/>
        </shape>
    </item>
    <!--其他状况-->
    <item>
        <shape>
            <stroke android:width="5px" android:color="@android:color/holo_blue_bright"/>
            <corners android:radius="15dp"/>
            <solid android:color="@android:color/holo_blue_bright"/>
        </shape>
    </item>
</selector>

布局文件:

<?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:background="#ffffff">
    <Button
        android:id="@+id/btn_one"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@drawable/btn_bg1"
        android:text="按钮"
        android:textStyle="bold"
        android:textSize="24sp"/>
    <Button
        android:id="@+id/btn_two"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_marginTop="10dp"
        android:text="按钮不可用"
        android:textSize="24sp"
        android:textStyle="bold"
        />

</LinearLayout>

效果图:

技术分享

技术分享技术分享

 

android基本控件学习-----Button

标签:

原文地址:http://www.cnblogs.com/huolan/p/5095971.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!