Android基础控件

1、RadioGroup

单选框。

其中的按钮为控件:RadioButton

实际效果图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.admin.vip_03.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你喜欢谁?"
        android:textSize="24dp"
        />

    <RadioGroup
        android:id="@+id/who"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="他"
            />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="她"
            />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="它"
            />

    </RadioGroup>

</LinearLayout>

Android 单选按钮 STYLE android单选控件_Android

1.1 获取选中的按钮并Toast相应信息

private void initRadioGroup() {
        RadioGroup radioGroup = findViewById(R.id.who);

        //设置监听
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //通过checkedId找到被选中的按钮
                RadioButton radioButton = findViewById(checkedId);


                Toast toast = Toast.makeText(MainActivity.this, "我最喜欢:" + radioButton.getText(),
                        Toast.LENGTH_SHORT);

                //改变Toast的位置
                toast.setGravity(Gravity.TOP, 0, 0);
                toast.show();
            }
        });
    }

Android 单选按钮 STYLE android单选控件_Android 单选按钮 STYLE_02

2、 CheckBox

复选框

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.admin.vip_03.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你喜欢谁?"
        android:textSize="24dp"
        />

    <CheckBox
        android:id="@+id/him"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="他"/>


    <CheckBox
        android:id="@+id/her"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="她"/>


    <CheckBox
        android:id="@+id/it"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="它"/>

</LinearLayout>

首先要在MainActivity中实现CompoundButton.OnCheckedChangeListener接口。

private void initCheckBox() {
        CheckBox him = findViewById(R.id.him);
        CheckBox her = findViewById(R.id.her);
        CheckBox it = findViewById(R.id.it);

        him.setOnCheckedChangeListener(this);
        her.setOnCheckedChangeListener(this);
        it.setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(buttonView.isChecked()) {

            Toast toast = Toast.makeText(MainActivity.this, buttonView.getText(),
                    Toast.LENGTH_SHORT);

            toast.setGravity(Gravity.TOP, 0, 0);
            toast.show();
        }
        else {
            Toast toast = Toast.makeText(MainActivity.this, "我喜欢我自己",
                    Toast.LENGTH_SHORT);

            toast.setGravity(Gravity.TOP, 0, 0);
            toast.show();
        }
    }

Android 单选按钮 STYLE android单选控件_基础控件_03

2.1 改变图标

2.1.1 selector

首先将准备好的图片导入drawable目录中。同时在该目录下创建check_selector.xml文件。

Android 单选按钮 STYLE android单选控件_基础控件_04

Android 单选按钮 STYLE android单选控件_Android 单选按钮 STYLE_05

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--state_checked判断checkBox的状态-->
    <item
        android:state_checked="true"
        android:drawable="@drawable/checked"
        />

    <item
        android:state_checked="false"
        android:drawable="@drawable/unchecked"
        />
</selector>

最后在checkBox的属性加入选择器文件即可。

android:button="@drawable/check_selector"

2.1.2 style

找到res目录下的value文件夹下的style.xml文件。

加入以下代码。

<!-- style中的name为主题的名字
         item中的name为修改的控件属性
    -->
    <style name="CheckBoxTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:button">@drawable/check_selector</item>
    </style>

最后在CheckBox中加入style属性。

style="@style/CheckBoxTheme"

实际效果:

图标找的太大,见谅。

Android 单选按钮 STYLE android单选控件_Android_06

Android 单选按钮 STYLE android单选控件_Android 单选按钮 STYLE_07

3、 Spinner

3.1 xml文件写Spinner

首先在res/value目录下,新建arrays.xml文件。

Android 单选按钮 STYLE android单选控件_基础控件_08

文件内容:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="who">
        <item>他</item>
        <item>她</item>
        <item>它</item>
    </string-array>
</resources>

Spinner控件的entries属性中,引用array.xml文件中的string-array

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你喜欢谁?"
        android:textSize="30dp"
        />

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/who"
        />

Android 单选按钮 STYLE android单选控件_Android 单选按钮 STYLE_09

Spinner的另一种显示方式:dialog

android:spinnerMode="dialog"

效果:

Android 单选按钮 STYLE android单选控件_android_10

3.2 java文件写Spinner

3.2.1 ArrayAdapter

<Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
</Spinner>

同时我们给Spinner加个监听器

private void initSpinner() {
        mSpinner = findViewById(R.id.spinner);

        String[] string = {"他", "她", "它"} ;
        //设置适配器
        ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this, //上下文
                android.R.layout.simple_list_item_1, //布局
                string); //文件

        mSpinner.setAdapter(arrayAdapter);

        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast toast = Toast.makeText(MainActivity.this,
                        "我喜欢" + parent.getSelectedItem().toString(),
                        Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.TOP, 0, 0);

                Toast toast1 = Toast.makeText(MainActivity.this,
                        "我喜欢" + string[position],
                        Toast.LENGTH_SHORT);
                toast1.setGravity(Gravity.CENTER, 0, 0);

                Toast toast2 = Toast.makeText(MainActivity.this,
                        "我喜欢" + arrayAdapter.getItem(position),
                        Toast.LENGTH_SHORT);
                toast2.setGravity(Gravity.BOTTOM, 0, 0);

                toast.show();
                toast1.show();
                toast2.show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Toast.makeText(MainActivity.this,"Nothing", Toast.LENGTH_SHORT).show();
            }
        });
    }

以上三种方式都能正确的吐司出选中的条目。

3.2.2 SimpleAdapter

本次我们用自己写的布局。

首先在layout文件夹下创建新的文件:list.xml

<?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="horizontal"
    android:gravity="center_horizontal"
    >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher_round"
        />
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="条目"
        android:layout_marginLeft="20dp"
        android:textSize="20dp"
        />

</LinearLayout>

Android 单选按钮 STYLE android单选控件_基础控件_11

private void initSpinner() {
        Spinner spinner = findViewById(R.id.spinner);

        List<Map<String, Object>> listMap = new ArrayList<>();

        for(int i = 0; i < 5; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("picture", R.mipmap.ic_launcher_round);
            map.put("text", "条目" + i);
            listMap.add(map);
        }

        //配置适配器
        //String数组对应map中的String
        //int数组对应list.xml中的控件id
        //注意数组中,数据的顺序不能调换
        SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this,
                listMap,
                R.layout.list,
                new String[] {"picture", "text"},
                new int[] {R.id.image, R.id.text}
        );

        spinner.setAdapter(simpleAdapter);

    }

Android 单选按钮 STYLE android单选控件_xml_12

3.2.3 BaseAdapter

private void initSpinner() {
        Spinner spinner = findViewById(R.id.spinner);

        BaseAdapter baseAdapter = new BaseAdapter() {
            @Override
            //条目总数
            public int getCount() {
                return 7;
            }

            @Override
            public Object getItem(int position) {
                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = convertView.inflate(MainActivity.this,
                        R.layout.list,
                        null);
                return view;
            }
        };

        spinner.setAdapter(baseAdapter);
    }

Android 单选按钮 STYLE android单选控件_基础控件_13

4、常用写法

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context="com.example.admin.vip_04.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你喜欢谁?"
        android:textSize="30dp"
        />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />


</LinearLayout>

mn_list.xml

<?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="horizontal"
    android:gravity="center_horizontal"
    >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        />

</LinearLayout>

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

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

        initSpinner();
    }

    private void initSpinner() {
        Spinner spinner = findViewById(R.id.spinner);

        List<Meinv> list = new ArrayList<>();

        for(int i = 0; i < 5; i++) {
            Meinv meinv = new Meinv();
            meinv.setId(i);
            meinv.setTui(120);
            meinv.setXiong("E");
            list.add(meinv);
        }

        SpinnerAdapter adapter = new SpinnerAdapter(list, MainActivity.this);

        spinner.setAdapter(adapter);
    }
}

Meinv.java

package com.example.admin.vip_04;

public class Meinv {
    private String xiong;
    private int tui;
    private int id;

    public String getXiong() {
        return xiong;
    }

    public void setXiong(String xiong) {
        this.xiong = xiong;
    }

    public int getTui() {
        return tui;
    }

    public void setTui(int tui) {
        this.tui = tui;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Meinv{" +
                "xiong='" + xiong + '\'' +
                ", tui=" + tui +
                "cm, id=" + id +
                '}';
    }
}

SpinnerAdapter.java

package com.example.admin.vip_04;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * @author YiJiang
 * @date 2018-03-17
 */

public class SpinnerAdapter extends BaseAdapter{
    private List<Meinv> mMeinvList;
    private Context mContext;

    public SpinnerAdapter(List<Meinv> meinvList, Context context) {
        mMeinvList = meinvList;
        mContext = context;
    }

    @Override
    public int getCount() {
        return mMeinvList.size();
    }

    @Override
    public Object getItem(int position) {
        return mMeinvList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = View.inflate(mContext, R.layout.mn_list, null);

        TextView textView = convertView.findViewById(R.id.text);
        textView.setText(mMeinvList.get(position).toString());
        return convertView;
    }
}

实际效果:

Android 单选按钮 STYLE android单选控件_xml_14