onClick为单选按钮设置处理程序。添加一个 EditText 用于输入名称
添加多行EditText
使用键盘输入电话号码
在一个 EditText 中组合输入类型
添加一个 RadioGroup 和单选按钮
列出
RadioButton元素的顺序决定了它们在屏幕上出现的顺序。
添加单选按钮单击处理程序
到这里布局:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.droidcafe.OrderActivity">
<TextView
android:id="@+id/order_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="8dp"
android:text="@string/order_label_text"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/name_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="32dp"
android:text="@string/name_label_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/order_textview" />
<EditText
android:id="@+id/name_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:ems="10"
android:hint="@string/enter_name_hint"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@+id/name_label"
app:layout_constraintStart_toEndOf="@+id/name_label" />
<TextView
android:id="@+id/address_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:text="@string/address_label_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/name_label" />
<EditText
android:id="@+id/address_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:ems="10"
android:hint="@string/enter_address_hint"
android:inputType="textMultiLine"
app:layout_constraintBaseline_toBaselineOf="@+id/address_label"
app:layout_constraintStart_toEndOf="@+id/address_label" />
<TextView
android:id="@+id/phone_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:text="@string/phone_label_string"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/address_text" />
<EditText
android:id="@+id/phone_text"
android:layout_width="134dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:ems="10"
android:hint="@string/enter_phone_hint"
android:inputType="phone"
app:layout_constraintBaseline_toBaselineOf="@+id/phone_label"
app:layout_constraintStart_toEndOf="@+id/phone_label" />
<TextView
android:id="@+id/note_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:text="@string/note_label_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/phone_label" />
<EditText
android:id="@+id/note_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:ems="10"
android:hint="@string/enter_note_hint"
android:inputType="textCapSentences|textMultiLine"
app:layout_constraintBaseline_toBaselineOf="@+id/note_label"
app:layout_constraintStart_toEndOf="@+id/note_label" />
<TextView
android:id="@+id/delivery_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:text="@string/choose_delivery_method"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/note_text" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/delivery_label">
<RadioButton
android:id="@+id/sameday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/same_day_messenger_service" />
<RadioButton
android:id="@+id/nextday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:onClick="onRadioButtonClicked"
android:text="@string/next_day_ground_delivery" />
<RadioButton
android:id="@+id/pickup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/pick_up" />
RadioGroup>
<Spinner
android:id="@+id/label_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/phone_text"
app:layout_constraintTop_toBottomOf="@+id/address_text" />
android.support.constraint.ConstraintLayout>
设置点击事件
/*
* Copyright (C) 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.droidcafe;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;
/**
* This activity is blank for now. Subsequent versions of the app
* provide input controls and a delivery method for an order.
*/
public class OrderActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked.
switch (view.getId()) {
case R.id.sameday:
if (checked)
// Same day service
displayToast(getString(R.string.same_day_messenger_service));
break;
case R.id.nextday:
if (checked)
// Next day delivery
displayToast(getString(R.string.next_day_ground_delivery));
break;
case R.id.pickup:
if (checked)
// Pick up
displayToast(getString(R.string.pick_up));
break;
default:
// Do nothing.
break;
}
}
public void displayToast(String message) {
Toast.makeText(getApplicationContext(), message,
Toast.LENGTH_SHORT).show();
}
}
运行程序

Spinner提供了一种从集合中选择一个值的快速方法。触摸Spinner屏幕会显示一个包含所有可用值的下拉列表,用户可以从中选择一个。如果您只提供两个或三个选项,如果您的布局中有空间供选择,您可能希望使用单选按钮;但是,如果有三个以上的选择,Spinner效果很好,可以根据需要滚动以显示项目,并且在布局中占用的空间很小。
提供一种选择电话号码标签的方法(例如Home、Work、Mobile或Other),您可以在 DroidCafe 应用程序的布局中添加一个微调器,OrderActivity以显示在电话号码字段的旁边。
在布局中添加Spinner

添加代码激活Spinner及其监听
/*
* Copyright (C) 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.droidcafe;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
/**
* This activity is blank for now. Subsequent versions of the app
* provide input controls and a delivery method for an order.
*/
public class OrderActivity extends AppCompatActivity
implements AdapterView.OnItemSelectedListener{
/**
* Sets the content view to activity_order, and gets the intent and its
* data. Also creates an array adapter and layout for a spinner.
*
* @param savedInstanceState Saved instance state bundle.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
// Get the intent and its data.
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = findViewById(R.id.order_textview);
textView.setText(message);
// Create the spinner.
Spinner spinner = findViewById(R.id.label_spinner);
if (spinner != null) {
spinner.setOnItemSelectedListener(this);
}
// Create an ArrayAdapter using the string array and default spinner
// layout.
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this,
R.array.labels_array,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears.
adapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner.
if (spinner != null) {
spinner.setAdapter(adapter);
}
}
/**
* Checks which radio button was clicked and displays a toast message to
* show the choice.
*
* @param view The radio button view.
*/
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked.
switch (view.getId()) {
case R.id.sameday:
if (checked)
// Same day service
displayToast(getString(
R.string.same_day_messenger_service));
break;
case R.id.nextday:
if (checked)
// Next day delivery
displayToast(getString(
R.string.next_day_ground_delivery));
break;
case R.id.pickup:
if (checked)
// Pick up
displayToast(getString(
R.string.pick_up));
break;
default:
// Do nothing.
break;
}
}
/**
* Displays the actual message in a toast message.
*
* @param message Message to display.
*/
public void displayToast(String message) {
Toast.makeText(getApplicationContext(), message,
Toast.LENGTH_SHORT).show();
}
// Interface callback for when any spinner item is selected.
@Override
public void onItemSelected(AdapterView<?> adapterView,
View view, int i, long l) {
String spinnerLabel = adapterView.getItemAtPosition(i).toString();
displayToast(spinnerLabel);
}
// Interface callback for when no spinner item is selected.
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
// Do nothing.
}
}
运行程序

以下android:inputType属性值会影响屏幕键盘的外观:
textAutoCorrect: 建议拼写更正。textCapSentences: 每个新句子都以大写字母开头。textPersonName:在用户键入时显示带有建议的单行文本,并在用户完成后点击**“完成”**键。textMultiLine:启用多行文本输入和返回键以添加新行。textPassword:输入密码时隐藏密码。textEmailAddress:显示电子邮件键盘而不是标准键盘。phone:显示电话键盘而不是标准键盘。在元素android:inputType的 XML 布局文件中设置属性值。EditText要组合值,请使用竖线 ( |) 字符连接它们。
单选按钮是输入控件,可用于从一组选项中仅选择一个选项:
RadioButton在 a 中将元素组合在一起, 以便 RadioGroup一次 RadioButton只能选择一个。RadioButton元素的顺序决定了它们在屏幕上出现的顺序。android:onClick每个属性RadioButton来指定点击处理程序。isChecked()方法, 如果选择了按钮,该方法Checkable将返回true。Spinner提供了一个下拉菜单:
Spinner在布局中添加一个 。ArrayAdapter将文本值数组分配为Spinner菜单项。AdapterView.OnItemSelectedListener接口,这还需要添加onItemSelected()和onNothingSelected()回调方法来激活Spinner和它的监听器。onItemSelected()回调方法检索Spinner菜单中的选定项,使用 getItemAtPosition().