Android表單UI及相應控件的事件處理

1、 Toast
Toast是一種輕量級的提示工具,可顯示一行文本對用戶的操做進行提示響應
用法: Toast.makeText(context,text,time).show();
context:上下文 、text:顯示文本內容、time:顯示時長
  Toast.LENGTH_SHORT(短期顯示)
  Toast.LENGTH_LONG(長時間顯示)
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ceshi.this, "你點擊了按鈕", Toast.LENGTH_SHORT).show();
}
});
 
2、 TextView——文本顯示控件
經常使用屬性
text    顯示的文本內容 textColor     文本顏色
textSize      字體大小 singleLine    單行顯示true|false
gravity  內容的重心位置 drawableLeft     文字左邊顯示的圖片
drawableRight    文字右邊顯示的圖片
drawableTop    文字上邊顯示的圖片
drawableBottom   文字下邊顯示的圖片
例:
<TextView
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#00ff00"
android:id="@+id/textView2"
android:layout_alignBottom="@id/f1"
android:layout_centerInParent="true"
android:text="來一碗魚丸粗麪"
android:textColor="#f00"
android:textSize="20sp"
android:gravity="bottom|center"/>
 
經常使用方法
nsetText()  設置顯示文字
nsetTextColor()  設置文字顏色,參數爲顏色值
nsetTextSize()  設置文字尺寸,參數1爲尺寸單位,2爲尺寸大小
ngetText()  獲取顯示的文字
 
3、 Button——按鈕
經常使用屬性
TextView的屬性
還有一個onClick,可是這個通常用不到,
Button的方法見上面的Toast的用法
 
4、 EditText——文本輸入控件,繼承自TextView
經常使用屬性
inputType     限制可以輸入文本類型
hint     顯示的提示    值爲 string
maxLength    可以輸入的最大長度  值爲 int
focusable    可否獲取焦點  值爲 boolean
lines     顯示的行數  值爲 int
maxLines 最大顯示行數  值爲 int
enable    是否可用   值爲 boolean
例:
 
<EditText
android:id="@+id/edittext_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入密碼!"
android:textSize="16sp"
android:textColor="#f00"
android:textColorHighlight="#a00000ff"
android:drawableLeft="@mipmap/ic_launcher"
android:inputType="textPassword" />
 
經常使用方法
setHint()  設置提示文本
setInputType()   設置輸入限制類型
setFocusable()  設置可否獲取焦點
setEnable()  設置是否可用
isEnable()  是否可用
具體代碼:
edittext1.getText().toString();//獲取EditView的文本值
edittext1.addTextChangedListener(watcher);//文本監聽
private TextWatcher watcher = new TextWatcher() {
/*文本改變以前*/
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
/*當文本改變的時候*/
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
/*文本改變以後*/
@Override
public void afterTextChanged(Editable s) {
//獲取已經輸入的的文字總數
int currentCount = s.length();
//計算還能輸入多少個字符,記住,數字不能直接寫
textview1.setText(String.valueOf(max - currentCount));
}
};
5、CheckBox——複選框,繼承自TextView
 
經常使用屬性
TextView的屬性
button  複選指示按鈕
checked  是否選中
例:
<CheckBox
android:id="@+id/checkbox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/checkbox1"
android:text="敲代碼" />
經常使用方法
TextView的方法
setChecked()  設置是否選中
isChecked()  是否選中的
setOnCheckedChangeListener()  設置選中改變監聽
具體代碼
checkboxall.setOnCheckedChangeListener(changeListener);
checkboxall.setChecked(false);
<!--佈局-->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/textviewtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="平時喜歡作什麼事情?" />

    <CheckBox
        android:id="@+id/checkboxall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/textviewtitle"
        android:layout_alignTop="@id/textviewtitle"
        android:layout_toRightOf="@id/textviewtitle"
        android:text="全選" />
    <!--內容的CheckBox-->
    <CheckBox
        android:id="@+id/checkbox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textviewtitle"
        android:layout_marginRight="80dp"
        android:text="玩遊戲" />

    <CheckBox
        android:id="@+id/checkbox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textviewtitle"
        android:layout_toRightOf="@+id/checkbox1"
        android:text="學習" />

    <CheckBox
        android:id="@+id/checkbox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/checkbox1"
        android:text="敲代碼" />

    <CheckBox
        android:id="@+id/checkbox4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/checkbox2"
        android:layout_toRightOf="@+id/checkbox1"
        android:text="跑步" />

    <CheckBox
        android:id="@+id/checkbox5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/checkbox3"
        android:text="游泳" />

    <CheckBox
        android:id="@+id/checkbox6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/checkbox4"
        android:layout_toRightOf="@+id/checkbox1"
        android:text="睡覺" />


    <TextView
        android:id="@+id/textviewinfo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/checkbox5"
        android:layout_marginTop="20dp"
        android:text="已選擇:"/>
</RelativeLayout>




<!--java代碼-->
package com.dc.work3;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by 怪蜀黍 on 2016/11/4.
 */

public class MainActivity2s extends AppCompatActivity {
    private CheckBox checkboxall;
    private CheckBox checkBox1;
    private CheckBox checkBox2;
    private CheckBox checkBox3;
    private CheckBox checkBox4;
    private CheckBox checkBox5;
    private CheckBox checkBox6;


    private TextView textviewinfo;
    private List<String> checkedStr;




    //操做取消一個時,全選取消,這個變量是是不是用戶點擊
    private boolean checkFoUser=true;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_2);

        checkboxall = (CheckBox) findViewById(R.id.checkboxall);
        checkBox1 = (CheckBox) findViewById(R.id.checkbox1);
        checkBox2 = (CheckBox) findViewById(R.id.checkbox2);
        checkBox3 = (CheckBox) findViewById(R.id.checkbox3);
        checkBox4 = (CheckBox) findViewById(R.id.checkbox4);
        checkBox5 = (CheckBox) findViewById(R.id.checkbox5);
        checkBox6 = (CheckBox) findViewById(R.id.checkbox6);
        textviewinfo = (TextView) findViewById(R.id.textviewinfo);

        checkBox1.setOnCheckedChangeListener(changeListener);
        checkBox2.setOnCheckedChangeListener(changeListener);
        checkBox3.setOnCheckedChangeListener(changeListener);
        checkBox4.setOnCheckedChangeListener(changeListener);
        checkBox5.setOnCheckedChangeListener(changeListener);
        checkBox6.setOnCheckedChangeListener(changeListener);
        checkboxall.setOnCheckedChangeListener(changeListener);

        checkedStr=new LinkedList<>();

    }
    public CompoundButton.OnCheckedChangeListener changeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            switch (buttonView.getId()){
                case R.id.checkbox1:
                case R.id.checkbox2:
                case R.id.checkbox3:
                case R.id.checkbox4:
                case R.id.checkbox5:
                case R.id.checkbox6:
                    String str=buttonView.getText().toString();
                    if(isChecked){
                        checkedStr.add(str);
                    }else {
                        checkedStr.remove(str);
                    }
                     checkboxall.setOnCheckedChangeListener(null);
                    if(checkBox1.isChecked()&&checkBox2.isChecked()&&checkBox3.isChecked()&&checkBox4.isChecked()&&checkBox5.isChecked()&&checkBox6.isChecked()){
                        //表示若是都選中時,把全選按鈕也選中
                        checkboxall.setChecked(true);
                    }else {
                        //不然就全選按鈕去不選中,可是這樣會觸發checkboxall的監聽,會把全部的都取消掉
                        checkboxall.setChecked(false);
                    }
                     checkboxall.setOnCheckedChangeListener(changeListener);
                    break;
                case R.id.checkboxall:
                    if(checkFoUser) {
                        checkBox1.setChecked(isChecked);
                        checkBox2.setChecked(isChecked);
                        checkBox3.setChecked(isChecked);
                        checkBox4.setChecked(isChecked);
                        checkBox5.setChecked(isChecked);
                        checkBox6.setChecked(isChecked);
                        break;
                    }
            }
            StringBuffer sb=new StringBuffer();
            for(String str:checkedStr){
                sb.append(str+",");
            }
            if(sb.length()>0){
                //設置長度爲長度-1,去除最後的「,」
                sb.setLength(sb.length()-1);
            }
            textviewinfo.setText("已選擇:"+sb.toString());
        }
    };

}

 

 
6、RadioButtonRadioGroup——單選按鈕、單選組
RadioButton——單選按鈕,繼承自TextView
單獨使用和CheckBox同樣,常常和RadioGroup一塊兒使用,形
成單選效果
RadioGroup——單選組,繼承自LinearLayout
經常使用屬性:LinearLayout的屬性
經常使用方法
setOnCheckedChangeLinsener()  內部單選按鈕選中改變監聽
check()  選中指定id的子項
getCheckedRadioButtonId()  獲取選中的RadioButton的id
clearCheck()  清除選中
7、 ImageView——圖片控件
經常使用屬性
src     指定要顯示的圖片
adjustViewBounds   控件的大小僅僅顯示到圖片的範圍大小
maxHeight    最大高度
maxWidth    最大寬度
scaleType  縮放類型
nfitStart(等比例自適應大小在開頭的顯示)  fitEnd(等比例自適應大小在末尾顯示)
nfitXY(自適應控件xy大小)  fitCenter(等比例自適應大小在中間顯示)
ncenter(按原圖片大小在中心顯示)  centerCrop(等比例縮放至充滿控件)
ncenterInside(圖片小於控件寬高時,縮放顯示在中間,不然等比例縮放後顯示在中間) 
nmatrix(圖片不縮放,顯示在左上角,此時能夠經過setImageMatrix()方法對圖片進行操做)
<!--默認縮放:自適應
android:scaleType="fitCenter"-->
<!--center不縮放,顯示在中心-->
<!--若是是小圖片,不進行縮放,會在中心顯示
,若是是大的圖片,會進行縮放android:scaleType="centerInside"-->
<!--按照左上角對齊,android:scaleType="matrix"-->
<!--若是要想無論圖片多大,都不讓其顯示邊框
android:adjustViewBounds="true"-->
ImageView
android:id="@+id/iv"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:adjustViewBounds="true"
android:background="#ddd"

經常使用方法
經常使用方法
setImageResource()  設置顯示的圖片資源
setImageBitmap()  設置顯示的圖片位圖
setImageDrawable()  設置顯示的圖片Drawable
setScaleType()  設置縮放類型
具體代碼:
iv// 設置圖片
ic_launcher//設置縮放類型
CENTER
8、 Spinner——下拉列表控件
經常使用屬性
spinnerMode     下拉列表模式
entries    數據實體
如右圖,這個是自定義的佈局,具體方法:
1.在layout文件夾下建立一個新的文件layout_list_item.xml
該文件中的內容:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
 
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/textview"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
</LinearLayout>
該段代碼實現的具體佈局,如右圖:
 
經常使用方法:
setAdapter()  設置數據適配器
setOnItemSelectedListener()  設置當子項選中的監聽
getSelectedItem()  獲取選中項
getSelectedItemId()  獲取選中項id
getSelectedItemPosition()  獲取選中項的下標
getSelectedView()  獲取選中項視圖
 
接下來是ActivityArrayadapter.class中的文件代碼:
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_arrayadapter);
spinner = (Spinner) findViewById(R.id.spinner);
auto=(AutoCompleteTextView)findViewById(R.id.auto);
//調用android的佈局
//若是不使用系統的佈局,能夠自定義佈局
//系統佈局
// ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,                   自定義佈局
android.R.id.text1, new String[]{"aaabbb", "aabb",
"abfaa", "abcaa", "aaacccc"});
//使用本身的佈局,這個佈局中,選項的前邊會有圖片顯示
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,
R.layout.layout_list_item,R.id.textview,
new String[]{"aaa", "aaa", "aaa", "aaa"});
spinner.setAdapter(adapter);
auto.setAdapter(adapter);
//這些是解決若是下邊提示沒有時,上邊的會沒有顯示
spinner.setOnItemSelectedListener(itemSelectedLis);
                      系統佈局
}
9、 ArrayAdapter——數據適配器
在進行數據顯示的時候,常常有大量重複的格式的數據要顯示,此時如果一個一個的設置數據會很是麻煩,適配器就是將這樣重複的格式的數據自動進行數據的設置的工具
上面的ActivityArrayadapter.class中的文件代碼就用到了適配器,請參考:
用法:
ArrayAdapter<String> adapter = new
ArrayAdapter<String>context,layoutRes,textviewId,data);
 
context:上下文  layoutRes:佈局資源 
textviewId:textview控件的id data:數據
在須要適配數據的控件調用其setAdapter()方法設置數據
10、 AutoCompleteTextView——建議提示輸入框
 
經常使用屬性:
completionHint   設置出如今下拉菜單中的提示標題
completionThreshold   設置用戶至少輸入多少個字符纔會顯示提示
dropDownHeight   下拉菜單的高度
dropDownWidth   下拉菜單的寬度
<!--建議提示輸入框-->
<AutoCompleteTextView
android:id="@+id/auto"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
 
經常使用方法:
EditText的方法
setAdapter()  設置數據適配器
auto=(AutoCompleteTextView)findViewById(R.id.auto);
auto.setAdapter(adapter);
 
十一 滾動條的基本使用
Ⅰ: ScrollView——豎向滾動條
Ⅱ: HorizontalScrollView——橫向滾動條
經常使用屬性:
scrollbars    滾動條
Horizontal:橫向滾動條  vertical:豎向滾動條  none  :無滾動條
注意:滾動條裏有且只能有一個子控件
<!--注意,ScrolView只能有一個子控件,要想有多個空間,使用Linear Layout包裹-->
<!--雖然高度是充滿,可是他是包裹的-->
<!--android:scrollbars="none"無滾動條-->
<!--android:overScrollMode="never" 滾動條到頭的提示模式-->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:overScrollMode="never">
12、ProgressBar——進度條
經常使用屬性:
style  設置進度條樣式
max  設置進度條最大值
progress  設置進度條進度值
secondaryProgress  第二進度條
maxHeight/maxWidth  最大寬高
minHeight/minWidth  最小寬高
progressDrawable  設置進度條圖片
 
<!--android:indeterminate="true" 設置進度條不肯定值的進度條-->
<!--android:progressDrawable="@drawable/progress_bar_drawable"
咱們自定義的進度條-->
<!--高度最大最小android:maxHeight="2dp"
 
android:minHeight="2dp"-->
<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="10"
android:progress="4"
android:secondaryProgress="6"
android:progressDrawable="@drawable/progress_bar_drawable"
/>
<!--自定義佈局-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--注意順序不能改變,先寫的在最下邊顯示-->
<!--這一項咱們修改背景-->
<item android:id="@android:id/background" android:drawable="@mipmap/a"/>
<!--這一項咱們修改第二進度-->
<item android:id="@android:id/secondaryProgress" android:drawable="@mipmap/b"/>
<!--這一項咱們修改進度-->
<item android:id="@android:id/progress" android:drawable="@mipmap/c"/>
</layer-list>
經常使用方法:
setMax()  設置最大值
setProgress()  設置進度值
getProgress()  獲取進度值
setSecondaryProgress()  設置第二進度值
getSecondaryProgress()  獲取第二進度值
能夠實現點擊按鈕,增長進度和減小進度,包括下邊有些內容
都在這代碼中:
<!--佈局-->

<?xml version="1.0" encoding="utf-8"?>
<!--雖然高度是充滿,可是他是包裹的-->
<!--android:scrollbars="none"無滾動條-->
<!--android:overScrollMode="never" 滾動條到頭的提示模式-->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none"
    android:overScrollMode="never">


    <!--注意,ScrolView只能有一個子控件,要想有多個空間,使用Linear Layout包裹-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <!--橫向滾動條-->
        <!--由於是橫向滾動條,因此無論你寬度設置多少,他始終是充滿的-->
        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></HorizontalScrollView>

        <!--進度條-->
        <!--android:indeterminate="true" 設置進度條不肯定值的進度條-->
        <!--android:progressDrawable="@drawable/progress_bar_drawable"咱們自定義的進度條-->
        <!--高度
        android:maxHeight="2dp"
            android:minHeight="2dp"-->
        <ProgressBar
            android:id="@+id/progress_bar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="10"
            android:progress="4"
            android:secondaryProgress="6"
            android:progressDrawable="@drawable/progress_bar_drawable"
            />

        <!--能夠拖拽的進度條-->
        <!-- android:thumb="@mipmap/ic_launcher"自定義圖片-->
        <!-- android:thumbOffset="100dp" 偏移量,通常不會用到-->
        <!--android:thumbTint="#0ff"  着色,無論什麼圖片仍是默認,顏色都改變-->
        <!--鼠標懸浮上邊這句話時,他的意思是,21版本纔可使用,當前版本是16-->
        <SeekBar
            android:id="@+id/seek_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:thumb="@mipmap/ic_launcher"
            android:thumbTint="#0ff"
            android:progress="50"/>

        <!-- android:visibility="invisible" 默認隱藏-->
        <!--調整文本框的大小盡可能使用邊來改變-->
        <TextView
            android:id="@+id/progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="50dp"
            android:paddingRight="50dp"
            android:paddingTop="20dp"
            android:paddingBottom="20dp"
            android:background="#cdcdcd"
            android:text="0%"
            android:textSize="22sp"
            android:visibility="invisible"
            android:layout_gravity="center_horizontal"/>

        <!--注意使用時,寬度和高度都使用包裹-->
        <!--android:stepSize="1"每次增加1-->
        <!--android:isIndicator="true" 是否做爲指示器,好比QQ等級-->
        <!--style="?android:attr/ratingBarStyleSmall"-->
        <!--hdpi是mdpi的1.5倍,xmdpi是m的2倍-->
        <RatingBar

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numStars="5"
            android:rating="1.5"
            android:stepSize="0.5"
            android:progressDrawable="@drawable/rating_bar_drawable"
            android:minHeight="21dp"
            android:maxHeight="21dp"/>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:id="@+id/bt1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:text="減小"/>

            <Button
                android:id="@+id/bt2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="增長"/>
        </LinearLayout>

        <!--爲了看清楚效果,這裏咱們把高度設爲20dp,text設置多點字符,這樣就超出了屏幕-->
        <TextView
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</ScrollView>
具體功能實現的java代碼

package com.dc.work3;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Spinner;

/**
 * Created by 怪蜀黍 on 2016/11/4.
 */

public class MainActivity_radiobutton extends AppCompatActivity {
    private RadioGroup rg;

    private Spinner spinner;

    //重寫一個參數的處事方法
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_radiobutton);

        rg= (RadioGroup) findViewById(R.id.rg1);
        rg.setOnCheckedChangeListener(changeLis);
        //若是沒有任何選中項,返回-1
      //  rg.getCheckedRadioButtonId();
        //選中指定項
       // rg.check(R.id.rb1);
        //清除選中項
       // rg.clearCheck();


        ImageView img=(ImageView) findViewById(R.id.iv);
//        設置圖片
        img.setImageResource(R.mipmap.ic_launcher);
        //設置縮放類型
        img.setScaleType(ImageView.ScaleType.CENTER);

        spinner=(Spinner)findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(itemSelectedListener);

    }
    private AdapterView.OnItemSelectedListener itemSelectedListener =new AdapterView.OnItemSelectedListener() {
        @Override//當項選中時  參數1:觸發事件的控件 2:選中的視圖 3:選中視圖的id
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        }
        @Override//當沒有項選中時
        public void onNothingSelected(AdapterView<?> parent) {

        }
    };

    private RadioGroup.OnCheckedChangeListener changeLis=new RadioGroup.OnCheckedChangeListener() {
        @Override //參數2:選中者的id
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            //獲取選中項的數據
            String str=spinner.getSelectedItem().toString();
            //獲取選中項的下標
            int position=spinner.getSelectedItemPosition();
            //獲取選中項的id
            long id= spinner.getSelectedItemId();
            Log.e("aaaa",str+"========="+position+"========="+id);
        }
    };
}

 

 
十3、 SeekBar——拖動條
經常使用屬性:
max  最大值
progress  進度值
secondaryProgress  第二進度條
progressDrawable  進度圖片
thumb  拖拽按鈕
thumbOffset  拖拽按鈕位置補償
maxHeight/maxWidth    最大寬高
minHeight/minWidth  最小寬高
 
例:
<!--能夠拖拽的進度條-->
 
<!-- android:thumb="@mipmap/ic_launcher"自定義圖片-->
<!-- android:thumbOffset="100dp" 偏移量,通常不會用到-->
<!--android:thumbTint="#0ff" 着色,無論什麼圖片仍是默認,顏色都改變-->
<!--鼠標懸浮上邊這句話時,他的意思是,21版本纔可使用,當前版本是16-->
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:thumb="@mipmap/ic_launcher"
android:thumbTint="#0ff"
android:progress="50"/>
經常使用方法:
setProgress()  設置進度值
getProgress()  獲取進度值
setSecondaryProgress()  設置第二進度值
getSecondaryProgress()  獲取第二進度值
setSeekBarChangeListener()  設置seekbar拖動改變監聽
具體代碼: 在上一個代碼中
 
十4、 RatingBar——等級條
經常使用方法:
numStars  最大值
rating  等級值
stepSize  最小步進值
progressDrawable  進度圖片
isIndicator  是不是指示器
例:
<!--注意使用時,寬度和高度都使用包裹-->
<!--android:stepSize="1"每次增加1-->
<!--android:isIndicator="true" 是否做爲指示器,好比QQ等級-->
<!--style="?android:attr/ratingBarStyleSmall"-->
<!--hdpi是mdpi的1.5倍,xmdpi是m的2倍-->
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="1.5"
android:stepSize="0.5"
android:progressDrawable="@drawable/rating_bar_drawable"
android:minHeight="21dp"
android:maxHeight="21dp"/>
 
自定義的佈局
<!--注意:順序不能夠改變,先寫的在最下一層-->
<!--背景-->
<!--第二進度-->
<!--進度-->
<item
android:id="@android:id/background"
android:drawable="@mipmap/favour_icon"/>
<item
android:id="@android:id/secondaryProgress"
android:drawable="@mipmap/favour_icon"/>
<item
android:id="@android:id/progress"
android:drawable="@mipmap/favoured_icon"/>
相關文章
相關標籤/搜索