Android simple controls (two) CheckBox, ProgressBar, SeekBar

Part of this blog then the check box (CheckBox), progress bar (ProgressBar) and drag the bar (SeekBar) supplemented finished.

Check box (CheckBox)

Check box to provide the user a number of options, the corresponding class is: android.widget.CheckBox; it is that it is indirectly inherited android.widget.Button; that is, nature is also a button on the box it is actually like, and ToggleButton. In addition, the check box has a unique property: android: checked, the selected CheckBox property is a Boolean value.
Ado, direct look at an example:
Layout:

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

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="你选择了:"
        android:textSize="18sp" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv"
        android:layout_alignParentStart="true"
        android:text="语文" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/checkBox"
        android:layout_alignParentStart="true"
        android:text="数学" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/checkBox2"
        android:text="英语" />

</RelativeLayout>

Here Insert Picture Description
MainActivity.java written as follows:

package com.beihang.test;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
    public static final String TAG = "MainActivity";

    private TextView tv;
    private CheckBox ch1, ch2, ch3;

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

        initUI();
    }

    @Override
    public void onCheckedChanged(CompoundButton bt, boolean isChecked) {
        StringBuffer sb = new StringBuffer();
        CheckBox ckb = (CheckBox) bt;
        sb.append(ckb.getText());
        if (!isChecked) {
            sb.append("cancelled");
        }
        tv.setText(sb.toString());
    }

    private void initUI() {
        tv = findViewById(R.id.tv);
        ch1 = findViewById(R.id.checkBox);
        ch2 = findViewById(R.id.checkBox2);
        ch3 = findViewById(R.id.checkBox3);
        ch1.setOnCheckedChangeListener(this);
        ch2.setOnCheckedChangeListener(this);
        ch3.setOnCheckedChangeListener(this);
    }

}

Code logic is also very simple, is provided for each listener CheckBox provided here activity class itself; and override OnCheckedChanged () method. It should be noted here is: the CheckBox event handler is to achieve different CompoundButton.OnCheckedChangeListener interfaces, and RadioButton, RadioButton event handler to implement interfaces are RadioGroup.OnCheckedChangeListener. Their big event source differences, the former event source is CheckBox, while the latter event source is RadioGroup, not RadioButton.

Progress bar (ProgressBar)

Progress bar (ProgressBar) for feedback whether background tasks being processed or processed progress. The progress bar belongs android.widget.ProgressBar class, it inherits directly android.view.View class.
Here are some common attributes and methods ProgressBar:
Android: maximum value max set schedule.
android: progress set the current schedule.
android: secondaryProgress second layer of progress bar progress.
android: indeterminate set the indeterminate progress bar mode, true is uncertain mode. (Uncertain mode is not sure progress, but has been circling ...)

getMax () returns the maximum progress.
getProgress () Returns the progress value.
getSecondaryProgress () Returns the value of the progress of the progress bar of the second layer.
incrementProgressBy (int diff) is set to increase progress.
isIndeterminate () whether the indeterminate mode.
setIndeterminate (boolean) Set uncertain mode.
There are many types of progress bar, progress bar horizontal strips, a large circular progress bar, small circular progress bar, etc., for example here describes two.
Generally strip progress bar to indicate the progress of the task determined circle indicates the progress of the task can not be determined.
Layout is as follows:

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

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp" />

    <Button
        android:id="@+id/inc_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/progressBar"
        android:layout_alignParentLeft="true"
        android:text="INC" />

    <Button
        android:id="@+id/dec_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/progressBar"
        android:layout_toRightOf="@id/inc_btn"
        android:text="DEC" />

    <ProgressBar
        android:id="@+id/progressBar2"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/inc_btn"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="20dp"
        android:visibility="gone"/>

    <ToggleButton
        android:id="@+id/load_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/progressBar2"
        android:text="ToggleButton"
        android:textOff="加载"
        android:textOn="停止" />

</RelativeLayout>

Layout includes a progress bar and round bar-shaped progress bars, their state is adjusted by a button, wherein the progress bar strip by setting the progress button, circular indeterminate progress bar.
MainActivity.java as follows:

package com.beihang.test;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    public static final String TAG = "MainActivity";
    private Button incBtn,decBtn;
    private ToggleButton loadBtn;
    private ProgressBar pb1, pb2;

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

        initUI();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.inc_btn:
                pb1.incrementProgressBy(1);
                break;
            case R.id.dec_btn:
                pb1.incrementProgressBy(-1);
                break;
            case R.id.load_btn:
                if (!loadBtn.isChecked()) {
                    pb2.setVisibility(ProgressBar.GONE);
                } else {
                    pb2.setVisibility(ProgressBar.VISIBLE);
                }
        }
    }

    private void initUI() {
        incBtn = findViewById(R.id.inc_btn);
        decBtn = findViewById(R.id.dec_btn);
        loadBtn = findViewById(R.id.load_btn);
        incBtn.setOnClickListener(this);
        decBtn.setOnClickListener(this);
        loadBtn.setOnClickListener(this);

        pb1 = findViewById(R.id.progressBar);
        pb2 = findViewById(R.id.progressBar2);
    }

}

As used herein, the current value incrementProgressBy () method of adjusting the progress of the strip, of course, you can also display the current value. Visibility property by adjusting the circular progress bar to control whether it is visible, wherein ProgressBar.GONE invisible and does not take up space, arranged ProgressBar.VISIBLE visible, i.e. has a circular motion.
Here Insert Picture Description
As a result of the operation of FIG, schedule adjustment button, the control button circular progress bar is displayed, as we wish.

Drag the bar (SeekBar)

Drag the bar can drag the progress bar, which means that the user can adjust the progress of this space, often used in the player. Drag the column corresponding class is android.widget.SeekBar, it inherited a progress bar android.widget.ProgressBar, that is to say SeekBar also a progress bar. Further, by android: thumb slider attributes from style definition.
Also exemplified:
only the layout of a TextView and a SeekBar.

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

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="当前进度:"
        android:textSize="18sp" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv" />
</RelativeLayout>

Here is MainActivity.java file contents.

package com.beihang.test;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener{
    public static final String TAG = "MainActivity";
    private TextView tv;
    private SeekBar sb;

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

        initUI();
    }

    @Override
    public void onProgressChanged(SeekBar sb, int progress, boolean fromUser) {
        tv.setText("当前进度:" + progress + "%");
    }

    @Override
    public void onStartTrackingTouch(SeekBar sb) {
        Log.i(TAG, "开始拖动!");
    }

    @Override
    public void onStopTrackingTouch(SeekBar sb) {
        Log.i(TAG, "结束拖动!");
    }

    private void initUI() {
        tv = findViewById(R.id.tv);
        sb = findViewById(R.id.seekBar);
        sb.setOnSeekBarChangeListener(this);
    }

}

Note SeekBar SeekBar.OnSeekBarChangeListener listener need to implement the interface, this interface has three methods need override, namely: onProgressChanged (SeekBar sb, int progress , boolean fromUser); onStartTrackingTouch (SeekBar sb); onStopTrackingTouch (SeekBar sb). Can be used to monitor the situation drag bar is operated.
Results are as follows:
Here Insert Picture Description

to sum up

This introduction of the use of Android CheckBox, ProgressBar, SeekBar three kinds of controls, and used with the article on the Android relatively simple to introduce some controls are finished, there is a little more complex, such as: a list of class controls, dialogs and other type of control time subsequent padded.

Released eight original articles · won praise 3 · Views 2693

Guess you like

Origin blog.csdn.net/qq_41241926/article/details/105193885