android selector 开关,Android开发(3)

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

实验要求:

实现类似如下的用户注册界面,外观样式可以自行调整。

36d9924fa89bae041130d7ee9f4ef4e5.png

要求:

1、会员名、电话、生日、email 使用 EditText 实现,请运用正确的 inputType。

2、地址使用 spinner 实现。

3、偏好公开使用 ToggleButton 实现,当关闭时不显示下面的偏好选项。

4、区域用 AutoCompleteTextView 实现。

5、点击最下方的添加按钮,用 Toast 显示用户输入信息的汇总。

实验效果:

基本效果:

614d0fdfab5d8fcc2557f95a3b475605.png

测试spinner(下拉列表):

95e1367311e0bbb1d4b1065d930ea4fc.png

测试AutoCompleteTextView(自动补写文本框):

64afeaa6ed1231ddf225e4564867104f.png

测试ToggleButton(开关按钮):

d26779246538ab289a5ad70b52255f8f.png

测试输出所有信息的Toast:

966c6ea2eca8bfe58ccf7d08b69b693d.png

实验记录:

(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"

/>

1

2

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值