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属性:

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">
<!--    输入框-->
    <EditText
        android: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"
        />
<!--添加照片-->
    <TextView
        android: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"/>
<!--    分享-->
    <TextView
        android: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布局文件中添加普通按钮:

<Button
        android: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(){
    @Override
    public 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"/>
<!--        文字与按钮边界的间隔-->
        <padding
            android: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">
​
<ImageView
    android:id="@+id/Iv1"
    android:scaleType="fitXY"
    android:src="@drawable/top"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
<Button
    android: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 {
​
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=(Button)findViewById(R.id.Bt1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,
                        "您已授权登录开心消消乐",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

2-2.图片按钮(ImageButton)

图片按钮与普通按钮的使用方法基本相同,不过图片按钮使用<ImageButton>标记定义,并且可以为其指定android:src属性设置要显示的图片。

基本语法格式:

<ImageButton
        android: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布局文件中添加单选按钮的基本语法格式如下:

<RadioButton
             android: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组件的基本格式如下:

<RadioGroup
             android: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() {
            @Override
            public 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() {
            @Override
            public 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布局文件中添加复选框的基本语法格式如下:

<CheckBox
          android: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(){
            @Override
            public 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属性:

XML 属性描述
android:max设置进度条的最大值
android:progress指定进度条已完成的进度值
android:progressDrawable设置进度条轨道的绘制形式

进度条组件还提供了两个常用方法用于操作进度:

setProgress(int progress)方法:设置进度完成的百分比

incrementProgressBy(int diff)方法:设置进度条的进度增加或减少,当参数值为正数时,表示进度增加;为负数时,表示进度减少。

通过style属性为ProgressBar指定风格,常用style属性值如表:

XML 属性描述
?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">
​
<!--    水平进度条-->
    <ProgressBar
        android: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")
    @Override
    protected 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(){
            @Override
            public 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() {
            @Override
            public 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>标记添加,基本语法格式如下:

<SeekBar
         android: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(){
    @Override
    public void onStopTrackingTouch(SeekBar seekBar){
        //要执行的代码
    }
    @Override
    public 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">
<!--    设置一张山水图片-->
    <ImageView
        android:id="@+id/iv1"
        android:src="@mipmap/lijiang"
        android:layout_width="match_parent"
        android:layout_height="440dp"/>
<!--    定义一个拖动条-->
    <SeekBar
        android:id="@+id/sb1"
        android:max="255"
        android:progress="255"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
<!--    属性图片-->
    <ImageView
        android: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;//定义拖动条
    @Override
    protected 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() {
            //当拖动条的滑块位置发生改变时触发该方法
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                //动态改变图片的透明度
                imageView.setImageAlpha(i);
            }
​
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
​
            }
​
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
​
            }
        });
    }
}

3-3.星级评分条(RatingBar)

星级评分条与拖动条类似,都允许用户通过拖动的方式来改变进度,所不同的是,星级评分条是通过星星图案来表示进度的。通常情况下,使用星级评分条表示对某一事物的支持度或对某种服务的满意程度等。

在屏幕中添加星级评分条,可以在XML布局文件中通过<RatingBar>标记添加,基本语法格式如下:

<RatingBar
           属性列表
           >
</RatingBar>

RatingBar组件支持的XML属性如表:

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">
<!--    店铺评分-->
    <TextView
        android: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"/>
<!--    星级评分条-->
    <RatingBar
        android: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"/>
<!--    发表评价-->
    <Button
        android: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;//星级评分条
​
    @Override
    protected 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() {
            @Override
            public 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();
            }
        });
    }
}

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、Android显示GIF动画 GifView GifView 是一个为了解决android中现在没有直接显示gif的view,只能通过mediaplay来显示这个问题的项目,其用法和 ImageView一样,支持gif图片 使用方法:1-把GifView.jar加入你的项目。2-在xml中配置GifView的基本属性,GifView继承自View类,和Button、ImageView一样是一个UI控件。 如: 3-在代码中配置常用属性: // 从xml中得到GifView的句柄 gf1 = (GifView) findViewById(R.id.gif1); // 设置Gif图片源 gf1.setGifImage(R.drawable.gif1); // 添加监听器 gf1.setOnClickListener(this); // 设置显示的大小,拉伸或者压缩 gf1.setShowDimension(300, 300); // 设置加载方式:先加载后显示、边加载边显示、只显示第一帧再显示 gf1.setGifImageType(GifImageType.COVER); GifView的Jar包共有四个类: GifAction.java 观察者类,监视GIF是否加载成功 GifFrame.java 里面三个成员:当前图片、延时、下张Frame的链接。 GifDecoder.java 解码线程类 GifView.java 主类,包括常用方法,如GifView构造方法、设置图片源、延迟、绘制等。 2、Calendar.v0.5.0 是 Android 平台的一个日历显示组件。 3、CWAC EndlessAdapter 是 Android 上一个可以无限往下滑进行列表数据加载的控件。 4、Android Horizontal ListView 是 Android 上一个水平滑动的 ListView 组件。 5、Android ViewBadger 视图布局。 6、滑动刷新的ListView Android PullToRefresh 为 Android 应用提供一个向下滑动即刷新列表的功能,就两个目标文件。 7、pakerfeldt-android-viewflow 是 Android 平台上一个视图切换的效果库。ViewFlow 相当于 Android UI 部件提供水平滚动的 ViewGroup,使用 Adapter 进行条目绑定。 8、Android 导航菜单 RibbonMenu 是 Android 上的一个导航菜单组件。就三个目标文件,菜单项直接在 XML 中定义,可添加文本和图标。 9、AndroidUI工具包 android-ui-utils 是一个工具包用来帮助设计和开发 Android 用户界面,包含三个单独的工具:Android Asset Studio用户界面原型模具,Android 设计预览,时常需要重复确认程序版面设计状况的 Android App 开发者,应该会爱上这个轻量级的 Java 程序:Andorid Design Preview 工具,通过 USB 连接之后,只要简单的在计算机中选取您想要显示的程序版面范围,就可将镜像结果直接显示于手机装置之上。 10、Androidui开发类库 GreenDroid 是一个Androidui开发类库,能够使你的Android开发更加简便和快捷。 11、Android滑动式菜单 SlidingMenu 是 Android 上实现类似 Facebook 和 Path 2.0 滑动式菜单的组件。 12、AsyncImageView 是 Android 上的一个异步从网络上获取图片并进行浏览的开源组件,可自动在本地进行缓存。该项目是 GreenDroid 的一部分。 13、仿Path按钮动画效果 PathButton 仿照Path应用首页左下角的Button动画效果写了个简单的Demo,由于数学不好,坐标总是和理想有出入,只是大致实现了动画效果,若果有人能把坐标算对,那么修改我的demo就能做成类似Path的那种动画效果!希望大家出点力帮着优化一下,并分享出来! 14、Android Intent开发包 OpenIntents Ope

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值