Android表单UI及相应控件的事件处理
生活随笔
收集整理的這篇文章主要介紹了
Android表单UI及相应控件的事件处理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、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(); } }); 二、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()? 獲取顯示的文字 三、Button——按鈕 常用屬性 TextView的屬性 還有一個onClick,但是這個一般用不到, Button的方法見上面的Toast的用法 四、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)); } }; 五、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"><TextViewandroid:id="@+id/textviewtitle"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="30dp"android:text="平時喜歡做什么事情?" /><CheckBoxandroid: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--><CheckBoxandroid:id="@+id/checkbox1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/textviewtitle"android:layout_marginRight="80dp"android:text="玩游戲" /><CheckBoxandroid: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="學習" /><CheckBoxandroid:id="@+id/checkbox3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/checkbox1"android:text="敲代碼" /><CheckBoxandroid: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="跑步" /><CheckBoxandroid:id="@+id/checkbox5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/checkbox3"android:text="游泳" /><CheckBoxandroid: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="睡覺" /><TextViewandroid: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() {@Overridepublic 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());}};}
常用方法 常用方法 setImageResource()? 設置顯示的圖片資源 setImageBitmap()? 設置顯示的圖片位圖 setImageDrawable()? 設置顯示的圖片Drawable setScaleType()? 設置縮放類型 具體代碼: iv// 設置圖片 ic_launcher//設置縮放類型 CENTER
八、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); 系統布局 } 九、ArrayAdapter——數據適配器 在進行數據顯示的時候,經常有大量重復的格式的數據要顯示,此時若是一個一個的設置數據會非常麻煩,適配器就是將這樣重復的格式的數據自動進行數據的設置的工具 上面的ActivityArrayadapter.class中的文件代碼就用到了適配器,請參考: 用法: ArrayAdapter<String> adapter = new ArrayAdapter<String>context,layoutRes,textviewId,data); context:上下文? layoutRes:布局資源? textviewId:textview控件的id data:數據 在需要適配數據的控件調用其setAdapter()方法設置數據 十、 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"> 十二、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包裹--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><!--橫向滾動條--><!--因為是橫向滾動條,所以不管你寬度設置多少,他始終是充滿的--><HorizontalScrollViewandroid: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"--><ProgressBarandroid: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--><SeekBarandroid: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" 默認隱藏--><!--調整文本框的大小盡量使用邊來改變--><TextViewandroid: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倍--><RatingBarandroid: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"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/bt1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="減少"/><Buttonandroid:id="@+id/bt2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="增加"/></LinearLayout><!--為了看清楚效果,這里我們把高度設為20dp,text設置多點字符,這樣就超出了屏幕--><TextViewandroid:layout_width="20dp"android:layout_height="wrap_content"android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"/><Buttonandroid: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;//重寫一個參數的處事方法@Overrideprotected 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:選中視圖的idpublic 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:選中者的idpublic void onCheckedChanged(RadioGroup group, int checkedId) {//獲取選中項的數據String str=spinner.getSelectedItem().toString();//獲取選中項的下標int position=spinner.getSelectedItemPosition();//獲取選中項的idlong id= spinner.getSelectedItemId();Log.e("aaaa",str+"========="+position+"========="+id);}}; }
?
六、RadioButtonRadioGroup——單選按鈕、單選組 RadioButton——單選按鈕,繼承自TextView 單獨使用和CheckBox一樣,經常和RadioGroup一起使用,形 成單選效果 RadioGroup——單選組,繼承自LinearLayout 常用屬性:LinearLayout的屬性 常用方法 setOnCheckedChangeLinsener()? 內部單選按鈕選中改變監聽 check()? 選中指定id的子項 getCheckedRadioButtonId()? 獲取選中的RadioButton的id clearCheck()? 清除選中 七、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
八、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); 系統布局 } 九、ArrayAdapter——數據適配器 在進行數據顯示的時候,經常有大量重復的格式的數據要顯示,此時若是一個一個的設置數據會非常麻煩,適配器就是將這樣重復的格式的數據自動進行數據的設置的工具 上面的ActivityArrayadapter.class中的文件代碼就用到了適配器,請參考: 用法: ArrayAdapter<String> adapter = new ArrayAdapter<String>context,layoutRes,textviewId,data); context:上下文? layoutRes:布局資源? textviewId:textview控件的id data:數據 在需要適配數據的控件調用其setAdapter()方法設置數據 十、 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"> 十二、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包裹--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><!--橫向滾動條--><!--因為是橫向滾動條,所以不管你寬度設置多少,他始終是充滿的--><HorizontalScrollViewandroid: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"--><ProgressBarandroid: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--><SeekBarandroid: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" 默認隱藏--><!--調整文本框的大小盡量使用邊來改變--><TextViewandroid: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倍--><RatingBarandroid: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"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/bt1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="減少"/><Buttonandroid:id="@+id/bt2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="增加"/></LinearLayout><!--為了看清楚效果,這里我們把高度設為20dp,text設置多點字符,這樣就超出了屏幕--><TextViewandroid:layout_width="20dp"android:layout_height="wrap_content"android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"/><Buttonandroid: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;//重寫一個參數的處事方法@Overrideprotected 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:選中視圖的idpublic 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:選中者的idpublic void onCheckedChanged(RadioGroup group, int checkedId) {//獲取選中項的數據String str=spinner.getSelectedItem().toString();//獲取選中項的下標int position=spinner.getSelectedItemPosition();//獲取選中項的idlong id= spinner.getSelectedItemId();Log.e("aaaa",str+"========="+position+"========="+id);}}; }
?
十三、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拖動改變監聽 具體代碼:在上一個代碼中 十四、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"/>轉載于:https://www.cnblogs.com/dingxiansen/p/6040621.html
總結
以上是生活随笔為你收集整理的Android表单UI及相应控件的事件处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: kalibr源码解析之fov模型重投影误
- 下一篇: (SCI论文写作)三种高效的论文用公式编