基础UI控件3:SeekBar、ProgressBar、DataPicker(安卓开发学习笔记——13)

1.SeekBar:
(1)
请添加图片描述
(2)SeekBar代码示例:
Java代码:

package com.example.myapplication_2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = (TextView)findViewById(R.id.tv_view);
        SeekBar seekBar = (SeekBar)findViewById(R.id.seeker_bar);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {            
                textView.setText(String.valueOf(progress-100));  //将原来的0——200范围的值变为-100——100

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }
}

布局代码:

<?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"
    tools:context=".MainActivity">

   <SeekBar
       android:id="@+id/seeker_bar"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:max="200">

   </SeekBar>

   <TextView
       android:text="0"
       android:id="@+id/tv_view"
       android:layout_gravity="center"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">

   </TextView>



</LinearLayout>

2.ProgressSeekBar:
(1)
请添加图片描述
(2)代码:

<?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"
    tools:context=".MainActivity">

    <SeekBar
        android:id="@+id/my_seeker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:thumb="@mipmap/ic_launcher"
        android:max="200">

    </SeekBar>

    <TextView
        android:id="@+id/tv_seeker_value"
        android:layout_width="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_height="wrap_content">

    </TextView>

    <ProgressBar
        android:max="100"
        android:progress="50"
        android:id="@+id/progress"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


    </ProgressBar>

    <Button
        android:text="减小0"
        android:id="@+id/btn_de"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </Button>

    <Button
        android:text="增加0"
        android:id="@+id/btn_in"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </Button>


</LinearLayout>
package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    SeekBar seekBar;
    TextView textView;

    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        seekBar = (SeekBar)findViewById(R.id.my_seeker);
        textView = (TextView)findViewById(R.id.tv_seeker_value);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    textView.setText(String.valueOf(progress-100));

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

//        progressBar用法!!!!
        progressBar = (ProgressBar) findViewById(R.id.progress);
        findViewById(R.id.btn_de).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressBar.setProgress(progressBar.getProgress()-1);

            }
        });

        findViewById(R.id.btn_in).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressBar.setProgress(progressBar.getProgress()+1);

            }
        });


    }
}

3.DataPicker和TimePicker:
(1)
请添加图片描述
(2)示例代码:
java代码:

package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        DatePicker datePicker = findViewById(R.id.my_picker);
        datePicker.getYear();
        datePicker.getMonth();
        datePicker.getDayOfMonth();

        TimePicker timePicker = findViewById(R.id.my_TimePicker);
        timePicker.getCurrentHour();
        timePicker.getCurrentMinute();


}

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">

    <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"
        tools:context=".MainActivity">

        <DatePicker
            android:id="@+id/my_picker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </DatePicker>

        <TimePicker
            android:id="@+id/my_TimePicker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </TimePicker>

    </LinearLayout>

</ScrollView>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值