Android APK开发基础——UI之小部件

TextView

带图片: android:drawableLeft="@drawable/fanhui"

 

ImageView

 

Edittext

属性

输入限制最大字符长度:   android:maxLength="10"

限制输入内容类型:      android:inputType="number"
限制输入内容行数:android:lines="1"

监听状态变化

 

Button

1、按键监听

普通Button


ImageButton


ToggleButton

/**
 * ToggleButton
 */
ToggleButton mtogglebutton = findViewById(R.id.mtogglebutton);
mtogglebutton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO: 2017/11/20
        if(isChecked){
            Log.e(tag, "mtogglebutton被按下");
            Toast.makeText(BasicUIActivity.this, "ToggleButton被点击", Toast.LENGTH_LONG);
        }else{

        }
    }
});
<ToggleButton
    android:id="@+id/mtogglebutton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:layout_margin="20dp"
    android:layout_weight="1"
    android:checked="true"
    android:disabledAlpha="@android:integer/config_longAnimTime"
    android:textOff="关"
    android:textOn="开"
    android:textSize="25sp"/>


RadioButton 

效果图:

代码:

<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_gravity="center_horizontal"
    android:contentDescription="性别">

    <RadioButton
        android:id="@+id/rb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:layout_gravity="center_horizontal"
        android:text="男">
    </RadioButton>

    <RadioButton
        android:id="@+id/rb2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:layout_gravity="center_horizontal"
        android:text="女">
    </RadioButton>
</RadioGroup>
/**
 * 单选按钮RadioButton &RadioGroup
 *
 * RadioGroup设置默认选中,如果在xml 布局文件中需要控制一个RadioButton 默认选中,就需要给他设置一个id。
 * 如果不设置id 的话,就会导致该RadioButton 一直是选中状态。
 */
RadioGroup group = findViewById(R.id.radioGroup);
//绑定一个匿名监听器
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup arg0, int arg1) {
        // TODO Auto-generated method stub
        //获取变更后的选中项的ID
        int radioButtonId = arg0.getCheckedRadioButtonId();
        //根据ID获取RadioButton的实例
        RadioButton rb = findViewById(radioButtonId);
        //更新文本内容,以符合选中项
        Toast.makeText(BasicUIActivity.this, "您的性别是:" + rb.getText(), Toast.LENGTH_LONG);
        Log.e(tag, "您的性别是:" + rb.getText());
    }
});

 

2、按键效果实现

1、自定义drawable,使用 selector 和 shape

button_select.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_pressed" android:state_pressed="true"></item>
    <!--<item android:drawable="@drawable/btn_focused" android:state_focused="true"></item>-->
    <item android:drawable="@drawable/btn_normal"></item>
</selector>

btn_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <stroke
        android:width="1dp"
        android:color="#000000" />

    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />

    <corners android:radius="5dp" />

    <solid android:color="#6CA6CD" />
</shape>

btn_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <stroke
        android:width="1dp"
        android:color="#000000" />

    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />

    <corners android:radius="5dp" />

    <solid android:color="#FFFFFF" />

</shape>

2、button以button_select作为背景。
    <Button
        android:id="@+id/btn_contentprovider"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_select"
        android:text="无图的联系人" >
    </Button>

CheckBox

效果图:

代码:

<CheckBox
    android:id="@+id/beijing"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:checked="true"
    android:text="北京"
    />
/**
 * CheckBox
 */
CheckBox beijing = findViewById(R.id.beijing);
beijing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
    //给CheckBox设置事件监听
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
                                 boolean isChecked) {
        // TODO Auto-generated method stub
        if(isChecked){
            Log.e(tag, buttonView.getText()+"选中");
        }else{
            Log.e(tag, buttonView.getText()+"取消选中");
        }
    }
});

ProgressBar

进度条控件

 

Spinner

下拉列表选择框

mspinner = findViewById(R.id.spinner1);
mspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
                               int pos, long id) {

        String[] bps = getResources().getStringArray(R.array.bps);
        Toast.makeText(BasicUIActivity.this, "你点击的是:" + bps[pos], Toast.LENGTH_SHORT).show();

        switch (pos) {
            case 0:
                // TODO: 2017/12/11
                break;
            case 1:
                // TODO: 2017/12/11
                break;
            case 2:
                // TODO: 2017/12/11
                break;
            default:
                // TODO: 2017/12/11
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }
});

布局文件:

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/bps"
    android:spinnerMode="dialog"
    />
array.xml:
<string-array name="bps">
    <item>请选择用户类型</item>
    <item>爷爷</item>
    <item>奶奶 </item>
    <item>爸爸</item>
    <item>妈妈</item>
</string-array>

 

notification

1.从系统服务中获得通知管理器

2. Notification.Builder构建Notification

3.定义一个PendingIntent,点击Notification后启动一个Activity

4.通过通知管理器来发起通知,ID区分通知

 

源码地址:

https://github.com/yuanhhyuan/Notification

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值