Input Controls(输入控件)

输入控件

输入控件是应用中用户界面的一种交互式组件。Android提供了大量的可供在UI中使用的控件,例如按钮、文本框、滑块进度条、复选框、缩放按钮和开关按钮等等。

在UI中添加输入控件就像在 XML layout 中添加XML元素一样简单。例如,下面是一个带有文本框和按钮的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">
    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
    <Button android:id="@+id/button_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage" />
</LinearLayout>

每个输入控件都支持一套特定的输入事件,所以你可以在用户进入文本框或触碰按钮时捕获这些事件。

常用控件


下面是一些常用控件的列表,你可以在你的应用中使用它们。通过这些链接学习更多关于使用控件的知识。

注解:Android提供的控件比这里列出来的要多。浏览 android.widget 包可以发现更多控件。如果你的应用需要特殊的输入控件,你还可以构建自己的 custom components

Control Type Description Related Classes
Button 可以被用户按下或点击来执行某个操作的按钮。 Button
Text field 可编辑的文本框。你可以使用AutoCompleteTextView部件来创建一个可以提供自动完成建议的文本输入部件。 EditText,AutoCompleteTextView
Checkbox 可以由用户切换on/off的开关。当提供给用户一组不互斥的可选项时,你应该使用复选框。 CheckBox
Radio button 与复选框类似,但一组里只能选择一个选项。 RadioGroup 
RadioButton
Toggle button 带有亮度亮度指示器的on/off按钮。 ToggleButton
Spinner 允许用户从一组数据中选择一个的下拉列表。 Spinner
Pickers 供用户通过上/下按钮或滑动手势从一系列值中选择一个的对话框。使用DatePicker部件输入日期(月,天,年)或使用TimePicker部件输入时间(时,分,上午/下午),这些值会自动格式化为用户所在区域的格式。 DatePicker,TimePicker
好的,以下是在 C# 中使用 WinForms 的窗口控件(Form)实现在其中输入文本的代码示例: ```csharp private void Form1_Load(object sender, EventArgs e) { Label label1 = new Label(); label1.Text = "请输入文本:"; label1.Location = new Point(20, 20); this.Controls.Add(label1); TextBox textBox1 = new TextBox(); textBox1.Location = new Point(20, 50); this.Controls.Add(textBox1); Button button1 = new Button(); button1.Text = "确定"; button1.Location = new Point(20, 80); button1.Click += new EventHandler(button1_Click); this.Controls.Add(button1); } private void button1_Click(object sender, EventArgs e) { string inputText = textBox1.Text; // 在这里对输入的文字进行处理,比如输出到控制台或者显示在另一个控件中 Console.WriteLine(inputText); } ``` 在这里,我们创建了一个 Label 控件和一个 TextBox 控件,用于提示用户输入文本和接收用户输入的文本。然后,我们创建了一个 Button 控件,用于触发在其中输入文本后的处理操作。在 Button 控件的 Click 事件处理程序中,我们获取 TextBox 控件的 Text 属性,即为其中输入的文本。然后可以对这段文本进行处理,比如输出到控制台或者显示在另一个控件中。这里我使用了 Console.WriteLine 将文本输出到控制台中。 请注意,这里我们将这些控件都添加到窗口控件(Form)中,以便在窗口中显示。你还可以根据自己的需求添加其他控件或调整这些控件的位置和大小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值