完成的结果如下 :
当点击 左按钮时 最上边的显示栏更改为左 反之则为右 点击开关显示为开或者关
下边两个为显示加载的界面 在输入栏输入数值可以控制进度条的百分比并且显示在最上边 点击图片一二切换图片
勾选下边三个选项可以在最上方显示栏中显示出来 勾选的课程 可多选
选择评价等级会出现选择的等级。
源代码如下:
1 package com.example.uidemo; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.os.Bundle; 6 import android.text.TextUtils; 7 import android.view.View; 8 import android.widget.Button; 9 import android.widget.CheckBox; 10 import android.widget.CompoundButton; 11 import android.widget.EditText; 12 import android.widget.ImageView; 13 import android.widget.ProgressBar; 14 import android.widget.RadioGroup; 15 import android.widget.RatingBar; 16 import android.widget.SeekBar; 17 import android.widget.Switch; 18 import android.widget.TextView; 19 import android.widget.Toast; 20 21 public class MainActivity extends AppCompatActivity { 22 TextView display; 23 Button buttonleft,buttonright,button3; 24 Switch aSwitch; 25 ProgressBar progressBar; 26 EditText editText; 27 RadioGroup radioGroup; 28 ImageView imageView; 29 SeekBar seekBar; 30 CheckBox checkBoxyuwen,checkBoxshuxue,checkBoxyingyu; 31 RatingBar ratingBar; 32 String yuwen=""; 33 String shuxue=""; 34 String yingyu=""; 35 36 @Override 37 protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 setContentView(R.layout.activity_main); 40 display=findViewById(R.id.textView); 41 buttonleft=findViewById(R.id.button); 42 buttonright=findViewById(R.id.button2); 43 button3=findViewById(R.id.button3); 44 aSwitch=findViewById(R.id.switch1); 45 progressBar=findViewById(R.id.progressBar3); 46 editText=findViewById(R.id.editText); 47 radioGroup=findViewById(R.id.radioGroup); 48 imageView=findViewById(R.id.imageView); 49 seekBar=findViewById(R.id.seekBar); 50 checkBoxyuwen =findViewById(R.id.checkBox); 51 checkBoxshuxue=findViewById(R.id.checkBox2); 52 checkBoxyingyu=findViewById(R.id.checkBox3); 53 ratingBar=findViewById(R.id.ratingBar); 54 55 56 buttonleft.setOnClickListener(new View.OnClickListener() { 57 @Override 58 public void onClick(View v) { 59 display.setText(R.string.button1); 60 } 61 }); 62 buttonright.setOnClickListener(new View.OnClickListener() { 63 @Override 64 public void onClick(View v) { 65 display.setText(R.string.button2); 66 } 67 }); 68 69 aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 70 @Override 71 public void onCheckedChanged(CompoundButton buttonView, boolean b) { 72 if(b) { 73 display.setText("开"); 74 } 75 else{ 76 display.setText("关"); 77 } 78 } 79 }); 80 button3.setOnClickListener(new View.OnClickListener() { 81 @Override 82 public void onClick(View v) { 83 String s=editText.getText().toString(); 84 if(TextUtils.isEmpty(s)){ 85 s="0"; 86 } 87 progressBar.setProgress(Integer.valueOf(s)); 88 display.setText(s); 89 } 90 }); 91 92 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 93 @Override 94 public void onCheckedChanged(RadioGroup group, int i) { 95 if(i==R.id.radioButton){ 96 imageView.setImageResource(R.drawable.ic_launcher_background); 97 } else { 98 imageView.setImageResource(R.drawable.ic_launcher_foreground); 99 } 100 } 101 }); 102 103 seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 104 @Override 105 public void onProgressChanged(SeekBar seekBar, int i, boolean fromUser) { 106 display.setText(String.valueOf(i)); 107 } 108 109 @Override 110 public void onStartTrackingTouch(SeekBar seekBar) { 111 112 } 113 114 @Override 115 public void onStopTrackingTouch(SeekBar seekBar) { 116 117 } 118 }); 119 120 checkBoxyuwen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 121 @Override 122 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 123 if(isChecked){ 124 yuwen="语文"; 125 }else { 126 yuwen=""; 127 } 128 display.setText(yuwen+shuxue+yingyu ); 129 } 130 }); 131 132 checkBoxshuxue.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 133 @Override 134 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 135 if(isChecked){ 136 shuxue="数学"; 137 }else{ 138 shuxue=""; 139 } 140 display.setText(yuwen+shuxue+yingyu ); 141 } 142 }); 143 144 checkBoxyingyu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 145 @Override 146 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 147 if(isChecked){ 148 yingyu="英语"; 149 }else{ 150 yingyu=""; 151 } 152 display.setText(yuwen+shuxue+yingyu ); 153 } 154 }); 155 ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { 156 @Override 157 public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { 158 Toast.makeText(getApplicationContext(),String.valueOf(rating)+"星的评价!",Toast.LENGTH_SHORT).show(); 159 } 160 }); 161 } 162 }
首先确定变量,然后与前台的控件相互关联,然后在代码的帮助下实现操作