这里面所有的实例代码我都上传到Github上了,想要看的可以去这:https://github.com/Silence9102/Exercise_Android/tree/master/Exercise_UI_Control
TextView文本框组件
EditText编辑框组件
Button普通按钮组件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Button Test Click", Toast.LENGTH_SHORT).show();
}
});
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button_1);
button.setOnClickListener(this);
}
//重写onClick()方法
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button_1:
Toast.makeText(this, "Button Test Second Click", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
ImageButton图片按钮组件
RadioButton单选按钮组件
//单选按钮组标签,用于将几个单选按钮组件设置为同一组
//为RadioGroup设置改变监听器当单选按钮改变时获取单选按钮选中内容
final RadioGroup radioGroup = findViewById(R.id.RadioGroup1);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton radioButton = findViewById(i);
Toast.makeText(MainActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
}
});
//设置当单击某个按钮时获取单选按钮选中内容
Button testBtn = findViewById(R.id.TestBtn);
testBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i < radioGroup.getChildCount(); i++) {
RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
if (radioButton.isChecked()) {
//使用Toast测试输出
Toast.makeText(MainActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
break;
}
}
}
});
CheckedBox复选框组件
android:checked:设置是否为默认勾选状态,属性值为true或者false
使用监听器获取CheckedBox选中时获取相关信息:
//关于下面这个变量为什么要用final修饰看这个:https://www.cnblogs.com/dolphin0520/p/3811445.html
final CheckBox checkBox1 = findViewById(R.id.like1);
checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(checkBox1.isChecked()){
Toast.makeText(MainActivity.this, checkBox1.getText(),Toast.LENGTH_SHORT).show();
}
}
});
判断当设置的监听器触发时,某个复选框是否被选中,如果被选中,获取相关信息:
if(checkBox1.isChecked()){
checkedMsg+=checkBox1.getText().toString();
}
DatePicker日期选择器
使用监听器在日期选择器更改日期时获取更改的日期数据:
public class MainActivity extends AppCompatActivity {
int year,month,day;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatePicker datePicker = findViewById(R.id.datepicker);
//使用Calendar对象获取现在的时间和日期
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
//将datepicker初始化为现在的日期时间
datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) {
year = i;
month =i1;
day = i2;
show(i,i1,i2);
}
});
}
private void show(int year , int month , int day){
String str = year+"年"+(month+1)+"月"+day+"日";
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
}
}
TimePicker时间选择器
使用监听器在时间选择器更改时间时获取更改的时间数据:
TimePicker timePicker = findViewById(R.id.timepicker1);
timePicker.setIs24HourView(true);
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker timePicker, int i, int i1) {
Toast.makeText(MainActivity.this, i+"时"+i1+"分", Toast.LENGTH_SHORT).show();
}
});
Chronometer计时器
使用监听器在计时器到达指定时间执行相应命令
// setbase() 设置计时器的起始时间
// setFormat() 设置显示时间的格式
// start() 指定开始计时
// stop() 指定停止计时
// setOnChronometerTickListener() 为计时器绑定事件监听器,当计时器改变时触发该监听器
Chronometer chronometer = findViewById(R.id.chronometer);
//设置计时器开始计时时间为当前系统时间
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.setFormat("%s");
chronometer.start();
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
//当计时器到60秒时停止计时
if(SystemClock.elapsedRealtime()-chronometer.getBase()>=60000)
chronometer.stop();
}
});