8种机械键盘轴体对比
本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?
实验要求:
实现类似如下的用户注册界面,外观样式可以自行调整。
要求:
1、会员名、电话、生日、email 使用 EditText 实现,请运用正确的 inputType。
2、地址使用 spinner 实现。
3、偏好公开使用 ToggleButton 实现,当关闭时不显示下面的偏好选项。
4、区域用 AutoCompleteTextView 实现。
5、点击最下方的添加按钮,用 Toast 显示用户输入信息的汇总。
实验效果:
基本效果:
测试spinner(下拉列表):
测试AutoCompleteTextView(自动补写文本框):
测试ToggleButton(开关按钮):
测试输出所有信息的Toast:
实验记录:
(1)RadioButton(单选按钮):1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
android:id="@+id/group"
android:layout_width="230dp"
android:layout_height="50dp"
android:orientation="horizontal"
android:gravity="start"
app:layout_constraintTop_toBottomOf="@id/emailEdit"
app:layout_constraintStart_toEndOf="@id/sex"
>
android:id="@+id/man"
android:checked="true"
android:layout_width="60dp"
android:layout_height="50dp"
android:text="男"
android:textSize="20sp"
/>
android:id="@+id/female"
android:checked="false"
android:layout_width="60dp"
android:layout_height="50dp"
android:text="女"
android:textSize="20sp"
/>
12
3
4android:orientation="horizontal"
// RadioButton设置默认为选中状态
android:checked="true"
获取RadioGroup中RadioButton选中值的方法:1
2
3RadioGroupgroup= (RadioGroup) findViewById(R.id.group);
RadioButton radioButton = (RadioButton) findViewById(group.getCheckedRadioButtonId());
result = radioButton.getText().toString();
(2)Spinner(下拉列表):1
2
3
4
5
6
7
8
9
android:layout_width="100dp"
android:layout_height="50dp"
android:id="@+id/addressSpinner"
android:entries="@array/addressArray"
android:gravity="start"
app:layout_constraintTop_toBottomOf="@id/group"
app:layout_constraintStart_toEndOf="@id/address"
/>1
2// 为Spinner配备数组资源
android:entries="@array/addressArray"
获取Spinner当前值的方法:1
2Spinner addressSpinner = (Spinner) findViewById(R.id.addressSpinner);
result = addressSpinner.getSelectedItem().toString();
(3)ToggleButton(开关按钮):1
2
3
4
5
6
7
8
9
10
11
android:layout_width="100dp"
android:layout_height="50dp"
android:id="@+id/habitOpenToggle"
android:textOff=""
android:textOn=""
android:checked="false"
android:background="@drawable/selector"
app:layout_constraintTop_toBottomOf="@id/addressSpinner"
app:layout_constraintStart_toEndOf="@id/habitOpen"
/>1
2
3
4
5
6
7// 设置当ToggleButton状态为开,关的时候分别显示的文本
android:textOff=""
android:textOn=""
// 设置ToggleButton默认状态为关
android:checked="false"
// 设置ToggleButton的背景变化
android:background="@drawable/selector" // 后面xml
@drawable/selector:1
2
3
4
5
获取ToggleButton当前状态的方法:1
2ToggleButton togglebutton = (ToggleButton) findViewById(R.id.habitOpenToggle);
result = togglebutton.isChecked();
设置ToggleButton开关状态改变时的事件响应方法:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22ToggleButton togglebutton;
togglebutton = (ToggleButton) findViewById(R.id.habitOpenToggle);
togglebutton.setOnClickListener(new .OnClickListener() {
@Override
public void onClick(View v) {
if(togglebutton.isChecked()) {// 如果状态为开,设置爱好那一行的三个组件显示
habit = (TextView) findViewById(R.id.habit);
swimCheck = (CheckBox) findViewById(R.id.swimCheck);
gymCheck = (CheckBox) findViewById(R.id.gymCheck);
habit.setVisibility(View.VISIBLE);
swimCheck.setVisibility(View.VISIBLE);
gymCheck.setVisibility(View.VISIBLE);
} else {// 如果状态为关,设置爱好那一行的三个组件不显示
habit = (TextView) findViewById(R.id.habit);
swimCheck = (CheckBox) findViewById(R.id.swimCheck);
gymCheck = (CheckBox) findViewById(R.id.gymCheck);
habit.setVisibility(View.GONE);
swimCheck.setVisibility(View.GONE);
gymCheck.setVisibility(View.GONE);
}
}
});
(4)CheckBox(复选框):1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
android:id="@+id/swimCheck"
android:layout_width="80dp"
android:layout_height="50dp"
android:text="@string/swim"
android:visibility="gone"
android:textSize="20sp"
app:layout_constraintTop_toBottomOf="@id/habitOpenToggle"
app:layout_constraintStart_toEndOf="@id/habit"
/>
android:id="@+id/gymCheck"
android:layout_width="80dp"
android:layout_height="50dp"
android:text="@string/gym"
android:visibility="gone"
android:textSize="20sp"
app:layout_constraintTop_toBottomOf="@id/habitOpenToggle"
app:layout_constraintStart_toEndOf="@id/swimCheck"
/>1
2// 设置CheckBox默认状态为不显示
android:visibility="gone"
获取复选框当前状态的方法:1
2CheckBox swimCheck = (CheckBox) findViewById(R.id.swimCheck);
result = swimCheck.isChecked();
(5)AutoCompleteTextView(自动补写文本框):1
2
3
4
5
6
7
8
9
android:id="@+id/areaText"
android:layout_width="230dp"
android:layout_height="40dp"
android:hint="输入所在区域"
android:dropDownAnchor="@id/emailEdit"
app:layout_constraintTop_toBottomOf="@id/swimCheck"
app:layout_constraintStart_toEndOf="@id/area"
/>1
2
3
4// 设置文本提示词
android:hint="输入所在区域"
// 设置下拉菜单的定位“锚点”组件,如果没有指定该属性,将使用该TextView本身作为定位
android:dropDownAnchor="@id/emailEdit"
配备数据源的方法:1
2
3
4
5String[] counties = getResources().getStringArray(R.array.region_array);
areaText = (AutoCompleteTextView) findViewById(R.id.areaText);
areaText.setThreshold(1);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, counties);
areaText.setAdapter(adapter);
实验代码:
具体代码详见Github