常用控件
spinner(下拉菜单)
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
public class MainActivity extends AppCompatActivity { private Spinner s;
private String[] dataSource = new String[]{"a1","b2","c3"}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); s = (Spinner) findViewById(R.id.spinner);
//设置一个Adapter
s.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dataSource));
//sOnSe OnIt
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//在这里写监听事件
System.out.println("用户选择的是"+dataSource[position]);
} @Override
public void onNothingSelected(AdapterView<?> parent) {
//不写
}
});
}
}
DatePickerDialog(日期选择器)
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择日期" />
public class ChooseDate extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_date);
slove();
} public void slove() {
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(ChooseDate.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
String chooseDate = String.format("%04d-%02d-%02d",year,month+1,dayOfMonth);//"%04d"总长度4位,不够用0补齐
((Button) findViewById(R.id.button)).setText(chooseDate);
}
},2000,0,1).show();//要注意月份是从0~11
}
});
}
}
TimePickerDialog(时间选择器)
这个就和日期选择器很像了
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择时间" />
public class ChooseTime extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_date);
slove();
} public void slove() {
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new TimePickerDialog(ChooseTime.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String chooseTime = String.format("%02d:%02d",hourOfDay,minute);
((Button) findViewById(R.id.button)).setText(chooseTime);
}
},0,0,true).show();//True是否使用24小时制
}
});
}
}
RadioButton(单选框)
这个要注意的是,单选按钮不能单独存在,要放在RadioGroup里
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:textSize="18sp"
android:text="世界上最大的海洋是?"/> <RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content" > <RadioButton
android:id="@+id/rbA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="A.太平洋" /> <RadioButton
android:id="@+id/rbB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="B.北冰洋" />
</RadioGroup> </LinearLayout> <Button
android:id="@+id/chooseOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"/> </LinearLayout>
public class ChooseSingle extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_single);
slove();
} private void slove() {
findViewById(R.id.chooseOne).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton rb = (RadioButton) findViewById(R.id.rbA);
if(rb.isChecked()) {
Toast.makeText(ChooseSingle.this, "正确", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(ChooseSingle.this, "错误", Toast.LENGTH_SHORT).show();
}
}
});
}
}
CheckBox(多选框)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="你喜欢喝什么饮料?" /> <CheckBox
android:id="@+id/cb1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="冰峰" /> <CheckBox
android:id="@+id/cb2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="果啤" /> <CheckBox
android:id="@+id/cb3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="雪碧" /> <TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
package com.liwenchi.myapplication; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView; public class MainActivity extends AppCompatActivity { private CheckBox cb1,cb2,cb3;
private TextView tv; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
slove();
} public void slove() {
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
tv = (TextView) findViewById(R.id.tv2);
cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
onChanged();
}
});
cb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
onChanged();
}
});
cb3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
onChanged();
}
});
} public void onChanged() {
String s = "我喜欢";
if(cb1.isChecked()) {
s += ","+cb1.getText();
}
if(cb2.isChecked()) {
s += ","+cb2.getText();
}
if(cb3.isChecked()) {
s += ","+cb3.getText();
}
tv.setText(s);
}
}