Android_UI基础控件3

一、SeekBar可拖拽进度条

public class MainActivity extends AppCompatActivity {
    private SeekBar seekBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBar = findViewById(R.id.seekBar);
        //设置进度条最大值
        seekBar.setMax(100);
        //设置进度条当前值
        seekBar.setProgress(30);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                //在进度发生变化时被触发
                //1、当前绑定的seekbar对象
                //2、当前进度
                //3、是否为用户手动触发
                Log.i("progress","变化"+seekBar.getProgress()+"");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                //在开始拖拽进度条时被触发
                Log.i("progress","开始"+seekBar.getProgress()+"");//获取到当前进度条的进度
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //在结束拖拽时被触发
                Log.i("progress","结束"+seekBar.getProgress()+"");
            }
        });
    }
}

二、ProgressBar进度条



    
    
    
    
    
    
    
    



Android_UI基础控件3_第1张图片

在java代码中设置max值为

progressBar.setMax(100);

进度条累加

progressBar.incrementProgressBy(20);
progressBar.incrementSecondaryProgressBy(20);

三、RatingBar星级进度条

isIndicator 是否允许用户操作

numStars 星星个数

stepSize 每次操作单位刻度

public class MainActivity extends AppCompatActivity {
private RatingBar ratingBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ratingBar = findViewById(R.id.rb);
        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                //当前绑定的RatingBar对象
                //当前rating评分的进度
                //是否由用户触发
                System.out.println("当前ratingBar:评分"+rating+" 是否来自用户:"+fromUser+" 每次评分刻度:"+ratingBar.getStepSize());
            }
        });
    }
}

四、DatePicker和TimePicker 日期和时间选择器

public class MainActivity extends AppCompatActivity {
private TimePicker timePicker;
private DatePicker datePicker;
private TextView textView;
private int Year,Month,Day,Hour,Minute;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        getCurrentTime();
        setPicker();
    }

    private void setPicker() {
        //设置为24小时制
        timePicker.setIs24HourView(true);
        //设置时间为当前时间
        timePicker.setCurrentHour(Hour);//获取当前系统时间
        timePicker.setCurrentMinute(Minute);
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                //时间选择器发生改变时触发
                System.out.println("From TimePicker:"+hourOfDay+":"+minute);
                Hour=hourOfDay;
                Minute = minute;
                textView.setText("当前日期+时间:"+Year+"/"+Month+"/"+Day+" "+Hour+":"+Minute);
            }
        });
        datePicker.init(Year, Month, Day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                System.out.println("From DatePicker:"+year+"/"+(monthOfYear+1)+"/"+dayOfMonth);
                Year=year;
                Month=(monthOfYear+1);
                Day=dayOfMonth;
                textView.setText("当前日期+时间:"+Year+"/"+Month+"/"+Day+" "+Hour+":"+Minute);
//国外计时法月份从0开始所以要+1
            }
        });

    }

    private void getCurrentTime() {
        //获取系统当前时间
        Calendar calendar = Calendar.getInstance();
        Year = calendar.get(Calendar.YEAR);
        Month = calendar.get(Calendar.MONTH);
        Day = calendar.get(Calendar.DAY_OF_MONTH);
        Hour = calendar.get(calendar.HOUR);
        Minute = calendar.get(Calendar.MINUTE);

    }

    private void initView() {
        timePicker = findViewById(R.id.tp);
        datePicker = findViewById(R.id.dp);
        textView = findViewById(R.id.tv);

    }
}




    
    

    
    
    
    
    


你可能感兴趣的:(系统学习Android)