Android studio—UI控件(view控件)上机实验

新用到view中Radiogroup、radiobutton、peeseekBar、togglebutton、checkbox控件)

主要xml代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">


    <RadioGroup
        android:id="@id/mRadioGroup"
        android:layout_width="107dp"
        android:layout_height="134dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.042">

        <RadioButton
            android:id="@+id/mybeRadioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:minHeight="48dp"
            android:text="Maybe"
            tools:ignore="TouchTargetSizeCheck,TouchTargetSizeCheck" />

        <RadioButton
            android:id="@+id/yesRadioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Yes"
            tools:ignore="TouchTargetSizeCheck" />

        <RadioButton
            android:id="@+id/noRadioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="No"
            tools:ignore="TouchTargetSizeCheck" />
    </RadioGroup>

    <SeekBar
        android:id="@+id/painSeekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:max="10"
        app:layout_constraintTop_toBottomOf="@+id/mRadioGroup"
        tools:layout_editor_absoluteX="0dp" />

    <TextView
        android:id="@+id/resultTextView"
        android:layout_width="110dp"
        android:layout_height="36dp"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.769" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="58dp"
        android:layout_height="26dp"
        android:text="Result"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.218"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.414" />

    <ToggleButton
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:textOff="hide result"
        android:textOn="show result"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.94"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.405" />

    <CheckBox
        android:id="@+id/momCheckBoxID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="Mom"
        app:layout_constraintTop_toBottomOf="@+id/questionTextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.127"
        app:layout_constraintStart_toStartOf="parent" />

    <CheckBox
        android:id="@+id/dadCheckBoxID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dod"
        app:layout_constraintStart_toStartOf="@+id/momCheckBoxID"
        app:layout_constraintTop_toBottomOf="@+id/momCheckBoxID" />

    <CheckBox
        android:id="@+id/grandpaCheckBoxID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Grandpa"
        app:layout_constraintStart_toStartOf="@+id/dadCheckBoxID"
        app:layout_constraintTop_toBottomOf="@+id/dadCheckBoxID" />

    <TextView
        android:id="@+id/checkResultTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Drag seekbar to show you pain level"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/mRadioGroup"
        app:layout_constraintVertical_bias="0.024" />

    <TextView
        android:id="@+id/questionTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="who is the most people you like?"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.451"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.473" />

    <Button
        android:id="@+id/resultButtonID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RESULT"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.841"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.638" />


</androidx.constraintlayout.widget.ConstraintLayout>

Java代码如下:

package com.example.myapplication04;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ToggleButton;


public class MainActivity extends AppCompatActivity {
    private RadioGroup mRadioGroup;
    private RadioButton  mRadioButton;
    private SeekBar mPainSeekBar;
    private TextView mResultTextView;
    private ToggleButton mShowHideBtn;
    private Button mResultBtn;
    private CheckBox mMomCheckBox,mDadCheckBox,mGrandpaChackBox;
    private TextView mCheckResultTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRadioGroup = (RadioGroup) findViewById(R.id.mRadioGroup);
        mPainSeekBar = (SeekBar) findViewById(R.id.painSeekBar);
        mResultTextView = (TextView) findViewById(R.id.resultTextView);
        mShowHideBtn =(ToggleButton) findViewById(R.id.toggleButton);
        mResultBtn =(Button) findViewById(R.id.resultButtonID);
        mMomCheckBox =(CheckBox) findViewById(R.id.momCheckBoxID);
        mDadCheckBox =(CheckBox) findViewById((R.id.dadCheckBoxID));
        mGrandpaChackBox =(CheckBox) findViewById(R.id.grandpaCheckBoxID);
        mCheckResultTextView =(TextView) findViewById(R.id.checkResultTextView);


        mRadioGroup.setOnCheckedChangeListener((group,checkedId) -> {
            mRadioButton = (RadioButton) findViewById(checkedId);
            switch (mRadioButton.getId()){
                case R.id.yesRadioButton: {
                    Log.d("onCheckedChanged","yes");
                }
                break;
                case R.id.mybeRadioButton:{
                    Log.d("onCheckedChanged","maybe");
                }
                break;
                case R.id.noRadioButton:{
                    Log.d("onCheckedChanged","no");
                }
                break;
            }
        });


        mPainSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mResultTextView.setTextColor(getResources().getColor(R.color.black));
                mResultTextView.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if(seekBar.getProgress() > 7){
                    mResultTextView.setTextColor(getResources().getColor(R.color.black));
                }

            }
        });


        mShowHideBtn.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if( mShowHideBtn.isChecked() ){
                mResultTextView.setVisibility(View.INVISIBLE);
            }else{
                mResultTextView.setVisibility(View.VISIBLE);
            }
        });

        mResultBtn.setOnClickListener(v -> {
            StringBuffer mResult = new StringBuffer();
            mResult.append(mMomCheckBox.getText().toString() +":"+mMomCheckBox.isChecked() +"\n");
            mResult.append(mDadCheckBox.getText().toString() +":"+mDadCheckBox.isChecked() +"\n");
            mResult.append(mGrandpaChackBox.getText().toString() +":"+mGrandpaChackBox.isChecked() +"\n");

            mCheckResultTextView.setText(mResult);
        } );












    }
}

运行截图如下:

 感谢大家的关注和支持,觉得有用就点个赞吧,谢谢!

猜你喜欢

转载自blog.csdn.net/weixin_57398221/article/details/123930266