3.Android学习之常用UI组件(一)
目錄
3.常用UI組件(一)
1.文本類組件
1-1.文本框(TextView)
1-2.編輯框(EditText)
2.按鈕類組件
2-1.普通按鈕(Button)
2-2.圖片按鈕(ImageButton)
2-3.單選按鈕(RadioButton)
2-4.復選框(CheckBox)
3.進度條類組件
3-1.進度條(ProgressBar)
3-2.拖動條(SeekBar)
3-3.星級評分條(RatingBar)
3.常用UI組件(一)
1.文本類組件
TextView文本框組件,用于顯示文本
EditText編輯框組件,用于編輯文本
1-1.文本框(TextView)
在Android中,使用兩種方法向屏幕中添加文本框:
1.在XML布局文件中使用<TextView>標記添加;
基本語法格式為:
<TextView屬性列表> </TextView>2.在Java文件中通過new關鍵詞創建。
TextView支持的常用XML屬性:
| android:autoLink | 指定是否將指定格式的文本轉換為可單擊的超鏈接形式,其屬性值有none、web、email、phone、map和all |
| android:drawableBottom | 在文本框內文本的底部繪制指定圖像,該圖像可以是放在res\mipmap目錄下的圖片,通過“@mipmap/文件名(不包括文件的擴展名)”設置 |
| android:drawableLeft | 在文本框內文本的左側繪制指定圖像,該圖像可以是放在res\mipmap目錄下的圖片,通過“@mipmap/文件名(不包括文件的擴展名)”設置 |
| android:drawableStart | 在Android4.2中新增的屬性,在文本框內文本的左側繪制指定圖像,該圖像可以是放在res\mipmap目錄下的圖片,通過“@mipmap/文件名(不包括文件的擴展名)”設置 |
| android:drawableRight | 在文本框內文本的右側繪制指定圖像,該圖像可以是放在res\mipmap目錄下的圖片,通過“@mipmap/文件名(不包括文件的擴展名)”設置 |
| android:drawableEnd | 在Android4.2中新增的屬性,在文本框內文本的右側繪制指定圖像,該圖像可以是放在res\mipmap目錄下的圖片,通過“@mipmap/文件名(不包括文件的擴展名)”設置 |
| android:drawableTop | 在文本框內文本的頂部繪制指定圖像,該圖像可以是放在res\mipmap目錄下的圖片,通過“@mipmap/文件名(不包括文件的擴展名)”設置 |
| android:gravity | 設置文本框內文本的對齊方式,可選值有top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、center、fill、clip_vertical和clip_horizontal等。這些屬性也可以同時指定。各屬性值之間用豎線隔開。例如,要指定組件靠右下角對齊,可以使用屬性值right|bottom |
| android:hint | 設置當文本框中文本內容為空時,默認顯示的提示文本 |
| android:inputType | 指定當前文本框顯示內容的文本類型,其可選值有textPassword、textEmailAddress、phone和date等,可以同時指定多個,使用“|”分隔 |
| android:singleLine | 指定該文本框是否為單行模式,其屬性值為true或false,為true時表示該文本框不會換行,當文本框中的文本超過一行時,其超出部分將被省略,同時在結尾處添加“···” |
| android:text | 指定文本框中顯示的文本內容,可以直接在該屬性值中指定,也可以通過在string.xml文件中定義文本常量的方式指定 |
| android:textColor | 設置文本框內文本的顏色,其屬性值可以是#rgb、#argb、#rrggbb或者#aarrggbb格式指定的顏色值 |
| android:textSize | 設置文本框內文本字體大小,其屬性值由代表大小的數值和單位組成,其單位可以是dp、px、pt、sp和in等 |
| android:width | 指定文本框的寬度,其單位可以是dp、px、pt、sp和in等 |
| android:height | 指定文本框的高度,其單位可以是dp、px、pt、sp和in等 |
1-2.編輯框(EditText)
在XML布局文件中使用<EditText>標記添加編輯框基本語法格式為:
<EditText屬性列表> </EditText>EditText類是TextView的子類,TextView類支持的XML屬性同樣適用于EditText組件。
注: android:inputType屬性可以控制輸入框的顯示類型。如密碼框textPassword
獲取布局文件中ID為login的編輯框輸入內容:
EditText login=(EditText)findViewById(R.id.login); String loginText=login.getText().toString();例:
<?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"android:paddingBottom="16dp"android:paddingLeft="16dp"android:paddingRight="16dp"android:paddingTop="16dp"android:background="#EAEAEA"tools:context=".MainActivity"> <!-- ? 輸入框--><EditTextandroid:id="@+id/Et1"android:layout_width="match_parent"android:layout_height="wrap_content"android:lines="6"android:hint="說點什么吧..."android:padding="5dp"android:background="#FFFFFF"android:gravity="top"android:layout_marginBottom="10dp"android:inputType="textMultiLine"/> <!--添加照片--><TextViewandroid:id="@+id/Tv1"android:drawableLeft="@mipmap/addpicture"android:text="添加照片"android:drawablePadding="8dp"android:gravity="center_vertical"android:padding="8dp"android:background="#FFFFFF"android:textColor="#767676"android:layout_width="match_parent"android:layout_height="wrap_content"/> <!-- ? 分享--><TextViewandroid:id="@+id/Tv2"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@mipmap/bottom" /> ? </LinearLayout>2.按鈕類組件
Button:普通按鈕,用于觸發一個指定的事件
ImageButton:圖片按鈕,用于觸發一個指定的事件,該按鈕以圖像來表現
RadioButton:單選按鈕
CheckBox:復選按鈕
2-1.普通按鈕(Button)
通過<Button>標記在XML布局文件中添加普通按鈕:
<Buttonandroid:id="@id/ID號"android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="顯示文本"> </Button>注:Button是TextView的子類,TextView支持的屬性Button都支持
注:更改按鈕顏色:
將res/values/theme.xml中的
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">改為
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">為按鈕添加單擊事件監聽器:
1.
import android.view.View.OnClickListener; import android.widget.Button; Button login=(Button)findViewById(R.id.login);//通過ID獲取布局文件中添加的按鈕 login.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){//要執行的動作代碼} })2.
public void myClick(View v){//要執行的動作代碼 } android:onClick="myClick"例:
shape.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item><shape android:shape="rectangle"> <!-- ? ? ? 設置填充顏色--><solid android:color="#1FBAF3"/> <!-- ? ? ? 四個角的弧度半徑--><corners android:radius="5dp"/> <!-- ? ? ? 文字與按鈕邊界的間隔--><paddingandroid:left="15dp"android:right="15dp"android:top="10dp"android:bottom="10dp"/></shape> </item> </selector>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"android:background="#EFEFF4"android:paddingBottom="16dp"android:paddingLeft="16dp"android:paddingRight="16dp"android:paddingTop="16dp"tools:context=".MainActivity"> ? <ImageViewandroid:id="@+id/Iv1"android:scaleType="fitXY"android:src="@drawable/top"android:layout_width="match_parent"android:layout_height="wrap_content"/> <Buttonandroid:id="@+id/Bt1"android:background="@drawable/shape"android:text="授權并登錄"android:textColor="#FFFFFF"android:layout_marginTop="10dp"android:layout_width="match_parent"android:layout_height="wrap_content"/> </LinearLayout>MainActivity.java
package com.example.loginbutton; ? import androidx.appcompat.app.AppCompatActivity; ? import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; ? public class MainActivity extends AppCompatActivity { ?@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button=(Button)findViewById(R.id.Bt1);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Toast.makeText(MainActivity.this,"您已授權登錄開心消消樂",Toast.LENGTH_SHORT).show();}});} }2-2.圖片按鈕(ImageButton)
圖片按鈕與普通按鈕的使用方法基本相同,不過圖片按鈕使用<ImageButton>標記定義,并且可以為其指定android:src屬性設置要顯示的圖片。
基本語法格式:
<ImageButtonandroid:id="@id/ID號"android:layout_height="wrap_content"android:layout_width="wrap_content"android:src="@mipmap/圖片文件名"android:scaleType="縮放方式"> </ImageButton>android:scaleType屬性值說明:
| matrix | 使用matrix2方式進行縮放 |
| fitXY | 對圖片橫向、縱向獨立縮放,使該圖片完全適應于ImageButton,圖片的縱橫比可能會改變 |
| fitStart | 保持縱橫比縮放圖片,直到該圖片能完全顯示在ImageButton中,縮放完成后該圖片放在ImageButton的左上角 |
| fitCenter | 保持縱橫比縮放圖片,直到該圖片能完全顯示在ImageButton中,縮放完成后該圖片放在ImageButton的中間 |
| fitEnd | 保持縱橫比縮放圖片,直到該圖片能完全顯示在ImageButton中,縮放完成后該圖片放在ImageButton的右下角 |
| center | 把圖片放在ImageButton的中間,但不進行任何縮放 |
| centerCrop | 保持縱橫比縮放圖片,使圖片能完全覆蓋ImageButton |
| centerInside | 保持縱橫比縮放圖片,使ImageButton能完全顯示該圖片 |
注:如果為圖片按鈕設置了android:background屬性后它將不會隨著用戶動作而改變,如果要讓它隨著用戶動作而改變需要使用StateListDrawable資源對其進行設置。
2-3.單選按鈕(RadioButton)
在默認情況下,單選按鈕顯示為一個圓形圖標,并且在該圖標旁邊放置一些說明性文字。在程序中一般將多個單選按鈕放置在按鈕組中,使這些單選按鈕表現出某種功能,當用戶選中某個單選按鈕后,按鈕組中的其他按鈕將被自動取消選中狀態。
通過<RadioButton>在XML布局文件中添加單選按鈕的基本語法格式如下:
<RadioButtonandroid:text="顯示文本"android:id="@+id/ID號"android:checked="true|false"android:layout_width="wrap_content"android:layout_height="wrap_content"> </RadioButton>android:checked屬性為true時表示選中,為false時表示取消選中,默認值為false。
通常情況下,RadioButton組件需要與RadioGroup組件一起使用,組成一個單選按鈕組。在XML布局文件中,添加RadioGroup組件的基本格式如下:
<RadioGroupandroid:id="@+id/radioGroup1"android:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"><!--此處添加多個RadioButton組件--> </RadioGroup>獲取單選按鈕組中選中項的值:
1.在改變單選按鈕組的值時獲取
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rg1);radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup radioGroup, int checkedId) {RadioButton radioButton = (RadioButton) findViewById(checkedId);radioButton.getText();//獲取被選中的單選按鈕的值}});2.單擊其他按鈕時獲取
final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rg1);//獲取一個提交按鈕Button button = (Button) findViewById(R.id.bt1);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {for (int i=0;i<radioGroup.getChildCount();i++){//根據索引值獲取單選按鈕RadioButton radioButton=(RadioButton) radioGroup.getChildAt(i);//判斷單選按鈕是否被選中if(radioButton.isChecked()){radioButton.getText();//跳出for循環break;}}}});2-4.復選框(CheckBox)
在默認情況下,復選框顯示為一個方塊圖標,并且在該圖標旁邊放置一些說明性文字。與單選按鈕唯一的不同是,復選框可以進行多選設置,每一個復選框都提供“選中”和“不選中”兩種狀態。
通過<CheckBox>在XML布局文件中添加復選框的基本語法格式如下:
<CheckBoxandroid:text="顯示文本"android:id="@+id/ID號"android:layout_width="wrap_content"android:layout_height="wrap_content"> </CheckBox>由于使用復選框可以選中多項,所有為了確定用戶是否選擇了某一項,還需要為每一個選項添加事件監聽器。
如:
final CheckBox checkBox=(CheckBox) findViewById(R.id.cb1);checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {//判斷該復選框是否被選中if (checkBox.isChecked()){//獲取選中項的值checkBox.getText();System.out.println(checkBox.getText());}}});3.進度條類組件
①進度條組件(ProgressBar):顯示某個耗時操作完成的百分比的組件
②拖動條組件(SeekBar):允許用戶通過拖動滑塊來改變值的組件
③星級評分條(RatingBar):允許用戶通過拖動來改變進度,但是使用星星圖案表示進度的組件
3-1.進度條(ProgressBar)
當一個應用在后臺執行時,前臺界面不會有任何信息,這時用戶根本不知道程序是否在執行以及執行進度等,因此需要使用進度條來提示程序執行的進度。在Android中提供了兩種進度條一種是水平進度條,一種是圓形進度條。
在屏幕中添加進度條,可以在XML布局文件中通過<ProgressBar>標記添加,基本語法格式如下:
<ProgressBar屬性列表> </ProgressBar>ProgressBar組件支持的XML屬性:
| android:max | 設置進度條的最大值 |
| android:progress | 指定進度條已完成的進度值 |
| android:progressDrawable | 設置進度條軌道的繪制形式 |
進度條組件還提供了兩個常用方法用于操作進度:
setProgress(int progress)方法:設置進度完成的百分比
incrementProgressBy(int diff)方法:設置進度條的進度增加或減少,當參數值為正數時,表示進度增加;為負數時,表示進度減少。
通過style屬性為ProgressBar指定風格,常用style屬性值如表:
| ?android:attr/progressBarStyleHorizontal | 細的長條水平進度條 |
| ?android:attr/progressBarStyleLarge | 大圓形進度條 |
| ?android:attr/progressBarStyleSmall | 小圓形進度條 |
| @android:style/Widget.ProgressBar.Large | 大跳躍、旋轉畫面的進度條 |
| @android:style/Widget.ProgressBar.Small | 小條躍、旋轉畫面的進度條 |
| @android:style/Widget.ProgressBar.Horizontal | 粗的長條水平進度條 |
例:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:background="@mipmap/xxll"android:paddingBottom="16dp"android:paddingTop="16dp"android:paddingLeft="16dp"android:paddingRight="16dp"tools:context=".MainActivity"> ? <!-- ? 水平進度條--><ProgressBarandroid:id="@+id/pb1"style="@style/Widget.AppCompat.ProgressBar.Horizontal"android:layout_alignParentBottom="true"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"android:layout_marginBottom="60dp"android:max="100"android:layout_width="match_parent"android:layout_height="25dp"/> </RelativeLayout>MainActivity.java
package com.example.horizontalprogressbar; ? import androidx.appcompat.app.AppCompatActivity; ? import android.annotation.SuppressLint; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.WindowManager; import android.widget.ProgressBar; import android.widget.Toast; ? public class MainActivity extends AppCompatActivity {private ProgressBar horizonP;//水平進度條private int mProgressStatus = 0;//完成進度private Handler mHandler;//聲明一個用于處理消息的Handler類的對象 ?@SuppressLint("HandlerLeak")@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);horizonP = (ProgressBar) findViewById(R.id.pb1);//獲取水平進度條mHandler = new Handler(){@Overridepublic void handleMessage(Message msg){//0x111是自定義的消息代碼,通過它可以區分消息,以便進行不同的處理if (msg.what == 0x111){horizonP.setProgress(mProgressStatus);//更新進度}else {Toast.makeText(MainActivity.this,"耗時操作已經完成",Toast.LENGTH_SHORT).show();horizonP.setVisibility(View.GONE);//設置進度條不顯示,并且不占用空間}}};new Thread(new Runnable() {@Overridepublic void run() {//循環獲取耗時操作完成的百分比,直到耗時操作結束while (true){//獲取耗時操作的百分比mProgressStatus = doWork();//創建并實例化一個消息對象Message m = new Message();//當完成進度不到100時表示耗時任務未完成if (mProgressStatus<100){//設置代表耗時操作未完成的消息代碼m.what = 0x111;//發送消息mHandler.sendMessage(m);//當完成進度到達100時表示耗時操作完成}else {//設置代表耗時操作已經完成的消息代碼m.what = 0x110;//發送消息mHandler.sendMessage(m);//退出循環break;}}}//模擬一個耗時操作private int doWork() {//改變完成進度mProgressStatus +=Math.random()*10;try {//線程休眠200毫秒Thread.sleep(200);}catch (InterruptedException e){e.printStackTrace();//輸出異常信息}return mProgressStatus;//返回新的進度}}).start();//開啟一個線程} }3-2.拖動條(SeekBar)
拖動條與進度條類似,所不同的是,拖動條允許用戶拖動滑塊來改變值,通常用于實現對某種數值的調節。
在屏幕中添加拖動條,可以在XML布局文件中通過<SeekBar>標記添加,基本語法格式如下:
<SeekBarandroid:layout_height="wrap_content"android:id="@+id/seekBar1"android:layout_width="match_parent"> </SeekBar>SeekBar組件允許用戶改變滑塊的外觀,這可以使用android:thumb屬性實現,該屬性的屬性值為一個Drawable對象,該Drawable對象將作為自定義滑塊
由于拖動條可以被用戶控制,所以需要為其添加OnSeekBarChangeListener監聽器,其基本代碼如下:
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){@Overridepublic void onStopTrackingTouch(SeekBar seekBar){//要執行的代碼}@Overridepublic void onStartTrackingTouch(SeekBar seekBar){//要執行的代碼}public void onProgressChanged(SeekBar seekBar,int progress,boolean fromUser){//其他要執行的代碼} });注:在上面的代碼中,onProgressChanged()方法中的參數progress表示當前進度,也就是拖動條的值。
例:
activity_main.xml
<?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:paddingTop="16dp"android:paddingLeft="16dp"android:paddingRight="16dp"android:paddingBottom="16dp"tools:context=".MainActivity"android:orientation="vertical"> <!-- ? 設置一張山水圖片--><ImageViewandroid:id="@+id/iv1"android:src="@mipmap/lijiang"android:layout_width="match_parent"android:layout_height="440dp"/> <!-- ? 定義一個拖動條--><SeekBarandroid:id="@+id/sb1"android:max="255"android:progress="255"android:layout_width="match_parent"android:layout_height="wrap_content"/> <!-- ? 屬性圖片--><ImageViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:scaleType="fitXY"android:src="@mipmap/meitu"/> ? ? ? </LinearLayout>MainActivity.java
package com.example.seekbar; ? import androidx.appcompat.app.AppCompatActivity; ? import android.os.Bundle; import android.widget.ImageView; import android.widget.SeekBar; ? public class MainActivity extends AppCompatActivity { ?private ImageView imageView;//定義圖片private SeekBar seekBar;//定義拖動條@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageView=(ImageView) findViewById(R.id.iv1);//獲取圖片seekBar=(SeekBar) findViewById(R.id.sb1);//獲取進度條//為進度條設置監聽事件seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {//當拖動條的滑塊位置發生改變時觸發該方法@Overridepublic void onProgressChanged(SeekBar seekBar, int i, boolean b) {//動態改變圖片的透明度imageView.setImageAlpha(i);} ?@Overridepublic void onStartTrackingTouch(SeekBar seekBar) { ?} ?@Overridepublic void onStopTrackingTouch(SeekBar seekBar) { ?}});} }3-3.星級評分條(RatingBar)
星級評分條與拖動條類似,都允許用戶通過拖動的方式來改變進度,所不同的是,星級評分條是通過星星圖案來表示進度的。通常情況下,使用星級評分條表示對某一事物的支持度或對某種服務的滿意程度等。
在屏幕中添加星級評分條,可以在XML布局文件中通過<RatingBar>標記添加,基本語法格式如下:
<RatingBar屬性列表> </RatingBar>RatingBar組件支持的XML屬性如表:
| android:isIndicator | 指定該星級評分條是否允許用戶改變,true為不允許改變 |
| android:numStars | 指定該星級評分條總共有多少顆星 |
| android:rating | 指定該星級評分條默認的星級 |
| android:stepSize | 指定每次最少要改變多少個星級,默認為0.5個 |
3個常用方法:
getRating()方法:獲取星級,表示選中了幾顆星
getStepSize()方法:獲取每次最少要改變多少個星級
getProgress()方法:獲取進度,獲取到的進度值為getRating()方法返回值與getStepSize()方法返回值之商
例:
activity.main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:background="@mipmap/xing1"android:paddingTop="16dp"android:paddingBottom="16dp"android:paddingLeft="16dp"android:paddingRight="16dp"tools:context=".MainActivity"> <!-- ? 店鋪評分--><TextViewandroid:id="@+id/tv1"android:layout_marginBottom="100dp"android:text="店鋪評分"android:textSize="20sp"android:layout_above="@+id/rb1"android:layout_width="wrap_content"android:layout_height="wrap_content"/> <!-- ? 星級評分條--><RatingBarandroid:id="@+id/rb1"android:layout_marginBottom="40dp"android:numStars="5"android:rating="0"android:layout_above="@+id/bt1"android:layout_width="wrap_content"android:layout_height="wrap_content"/> <!-- ? 發表評價--><Buttonandroid:id="@+id/bt1"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:background="#FF5000"android:text="發表評價"android:layout_width="wrap_content"android:layout_height="wrap_content"/> ? ? </RelativeLayout>MainActivity.java
package com.example.starrating; ? import androidx.appcompat.app.AppCompatActivity; ? import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.RatingBar; import android.widget.Toast; ? public class MainActivity extends AppCompatActivity {private RatingBar ratingBar;//星級評分條 ?@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ratingBar=(RatingBar) findViewById(R.id.rb1);//獲取星級評分條Button button=(Button) findViewById(R.id.bt1);//獲取提交按鈕button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {int result = ratingBar.getProgress();//獲取進度float rating=ratingBar.getRating();//獲取等級float step = ratingBar.getStepSize();//獲取每次最少要改變多少個星級Log.i("星級評分條","step="+step+"result="+result+"rating="+rating);Toast.makeText(MainActivity.this, "你得到了"+rating+"顆星", Toast.LENGTH_SHORT).show();}});} }總結
以上是生活随笔為你收集整理的3.Android学习之常用UI组件(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python——pyqt5的计算器(源码
- 下一篇: kali网络问题解决办法