Android集训第二天

上午
android:password   用于隐藏其文本(目前所了解到)
android:maxLength  正如其字面意思,最大长度,超出部分不显示
android:autoLink   自我感觉这个东西很有用,不用向系统申请权限就直接打开网址等等

在面对很多相同属性的控件的时候我们可以选择样式style,把重复的属性抽象出来,在style.xml里面定义style

格式如下:

<style name="custom">                                 
        <item name="android:autoLink">all</item>
        <item name="android:password">false</item>
        <item name="android:maxLength">20</item>
        <item name="android:textColor">@color/red</item>
        ......
        ......
</style>

然后在你所需要这个style的控件里面添加style=”@style/custom”这个属性,其中custom是你自己定义的style文件名

最简单的事件处理方式

bt1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        tv.setText(R.string.second);
        ........
        ........
        }
    });

ImageButton(用代码说清楚点)

这是ImageButton的xml

<ImageButton
        android:id="@+id/image_bt1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:background="@drawable/button_state"/>

然后我们在res/drawable,右键new/Drawable resource files,编译器会自动给你匹配selector
你只需要填写名称即可,如果没有出现就新建一个file然后自己找selector这个得到

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false" android:drawable="@drawable/image2"/>
    <item android:state_pressed="true" android:drawable="@drawable/image3"/>
</selector>

中间两行代码是自己敲的,用于点击图片前后图片发生变化,然后与上面的@drawable/button_state对应

下午

CheckBox 允许多选

RadioButton只能单选,但要和RadioGroup一起使用

示例:

<CheckBox
        android:id="@+id/cb"
        android:text="成都"
           android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <CheckBox
        android:id="@+id/cb1"
        android:text="绵阳"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

这里允许选择成都和绵阳一同选择

<RadioGroup
       android:id="@+id/rg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <RadioButton
           android:id="@+id/rb_man"
           android:text="man"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    <RadioButton
            android:id="@+id/rg_woman"
            android:text="woman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    <RadioButton
            android:id="@+id/rg_animal"
            android:text="animal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
            ..........
            ..........
            ..........
            ..........
</RadioGroup>

这里只允许单选(man,woman,animal…….中其中之一)

但是我们怎么知道用户在RadioGroup选择了啥?

这就需要一个RadioGroup.OnCheckedChangeListener()方法,然后匹配xml里面的id

示例:

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){
                case R.id.rb_man:textView.setText("man is cheaked");break;
                case R.id.rg_woman:textView.setText("woman is cheaked");break;
                default:textView.setText("Nothing");break;
            }
        }
    });

配置列表项

1、直接通过资源文件配置

定义一个values/city_labels.xml文件,用元素指定,定义内容如下

示例:

<string-array name="city_labels">
    <item>成都</item>
    <item>绵阳</item>
    <item>南充</item>
</string-array>

然后在Spinner这个控件(下拉列表框)直接使用

android:entries=”@array/city_labels”

2、通过ArrayAdapeter类实现

讲了三种方法

public ArrayAdapter(Context context,int textViewResourceId,List<T> objects)
public ArrayAdapter(Context context,int textViewResourceId,List<T> objects)
public static ArrayAdapter<CharSequence>createFromResource
    (Context context,int textArrayResId,int textViewResourceId)

通过示例来学习(以下是一些主要代码)

首先定义

private ArrayAdapter<String> adapter;
private List<String> color_data;

color_data = new ArrayList<String>();
color_data.add("red");
color_data.add("blue");
color_data.add("green");
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, 
color_data);
spinner.setAdapter(adapter);

华丽的分隔符—————————————————————————————————————————————————————–

直接在java中写
private ArrayAdapter<String> adapter_edu;    
private String[] arry_edu = {"小学", "初中", "高中", "大学"};

adapter_edu = new ArrayAdapter(this, android.R.layout.simple_spinner
_dropdown_item, arry_edu);
spinner.setAdapter(adapter_edu);

华丽的分隔符—————————————————————————————————————————————————————–

先在values/strings.xml里面用<string-array>元素指定animal
 <resources>
    <string name="app_name">MySpinnerDemo</string>
    <string-array name="animal">
        <item>cat</item>
        <item>dog</item>
        <item>pig</item>
    </string-array>
</resources>
然后在在java里面写
adapter_animal = ArrayAdapter.createFromResource(this, R.array.animal, 
android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter_animal);

了解到Spinner这个控件是用AdapterView.OnItemSelectedListener()这个来实现监听

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position
        , long id) {
        //  if(position == 0){
                textView.setText(position+"点击");
        // }
                   .........
        }
    @Override
    public void onNothingSelected(AdapterView<?> parent) {
            textView.setText("Nothing");
        }
    });

下面的onNothingSelected没用过,不知道是什么玩意,百度上的解释是这样的:当我们的adapter为空的时候就会调用到这个方法