Android:日常学习笔记(7)———探究UI开发(1)

Android:日常学习笔记(7)———探究UI开发(1)

常用控件的使用方法

TextView

说明:TextView是安卓中最为简单的一个控件,常用来在界面上显示一段文本信息。

代码:

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:gravity="center"
        android:textSize="24sp"
        android:textColor="#00FF00"
         />

说明:

  1.match_parent表示让当前控件的大小与其父布局大小一样,也就是由父布局来决定当前控件的大小。

  2.wrap_content表示让控件中的内容决定控件的大小。

  3.android:gravity可以用来指定文本的对齐方式。

Button

说明:A button consists of text or an icon (or both text and an icon) that communicates what action occurs when the user touches it.

(按钮是一种可以包含文本图标文本和图标信息的用来反馈用户点击事件的控件)

代码:

  Depending on whether you want a button with text, an icon, or both, you can create the button in your layout in three ways:

  • With text, using the Button class:

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_text"
        ... />
  • With an icon, using the ImageButton class:

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/button_icon"
        ... />
  • With text and an icon, using the Button class with the android:drawableLeft attribute:

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_text"
        android:drawableLeft="@drawable/button_icon"
        ... />

可以加入下句代码解决所有字母都是大写的问题:

android:textAllCaps="false"

 让按钮响应事件

1.第一种方法:在XML种绑定事件

To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

For example, here‘s a layout with a button using android:onClick:

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/button_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage" />

Within the Activity that hosts this layout, the following method handles the click event:

/** Called when the user touches the button */
public void sendMessage(View view) {
    // Do something in response to button click
}

The method you declare in the android:onClick attribute must have a signature exactly as shown above. Specifically, the method must:

  • Be public
  • Return void
  • Define a View as its only parameter (this will be the View that was clicked)

2.第二种方法:Using an OnClickListener

You can also declare the click event handler programmatically rather than in an XML layout. This might be necessary if you instantiate the Button at runtime or you need to declare the click behavior in a Fragment subclass.

To declare the event handler programmatically, create an View.OnClickListener object and assign it to the button by calling setOnClickListener(View.OnClickListener). For example:

Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Do something in response to button click
    }
});

EditText

说明:

  Every text field expects a certain type of text input, such as an email address, phone number, or just plain text. So it‘s important that you specify the input type for each text field in your app so the system displays the appropriate soft input method (such as an on-screen keyboard).

  Beyond the type of buttons available with an input method, you should specify behaviors such as whether the input method provides spelling suggestions, capitalizes new sentences, and replaces the carriage return button with an action button such as a Done or Next. This lesson shows how to specify these characteristics

代码:

    <EditText
        android:id="@+id/phone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="默认的提示信息"
        android:maxLines="2"
        android:inputType="phone" />    

 说明:

android:inputType属性决定了这个文本框应该接收和信息哪一类信息。如上图输入发默认切换为 手机号码 显示。
Constant Value Description
date 14 For entering a date. Corresponds to TYPE_CLASS_DATETIME | TYPE_DATETIME_VARIATION_DATE.
datetime 4 For entering a date and time. Corresponds to TYPE_CLASS_DATETIME | TYPE_DATETIME_VARIATION_NORMAL.
none 0 There is no content type. The text is not editable.
number 2 A numeric only field. Corresponds to TYPE_CLASS_NUMBER | TYPE_NUMBER_VARIATION_NORMAL.
numberDecimal 2002 Can be combined with number and its other options to allow a decimal (fractional) number. Corresponds toTYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_DECIMAL.
numberPassword 12 A numeric password field. Corresponds to TYPE_CLASS_NUMBER | TYPE_NUMBER_VARIATION_PASSWORD.
numberSigned 1002 Can be combined with number and its other options to allow a signed number. Corresponds toTYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_SIGNED.
phone 3 For entering a phone number. Corresponds to TYPE_CLASS_PHONE.
text 1 Just plain old text. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_NORMAL.
textAutoComplete 10001 Can be combined with text and its variations to specify that this field will be doing its own auto-completion and talking with the input method appropriately. Corresponds to TYPE_TEXT_FLAG_AUTO_COMPLETE.
textAutoCorrect 8001 Can be combined with text and its variations to request auto-correction of text being input. Corresponds toTYPE_TEXT_FLAG_AUTO_CORRECT.
textCapCharacters 1001 Can be combined with text and its variations to request capitalization of all characters. Corresponds toTYPE_TEXT_FLAG_CAP_CHARACTERS.
textCapSentences 4001 Can be combined with text and its variations to request capitalization of the first character of every sentence. Corresponds to TYPE_TEXT_FLAG_CAP_SENTENCES.
textCapWords 2001 Can be combined with text and its variations to request capitalization of the first character of every word. Corresponds to TYPE_TEXT_FLAG_CAP_WORDS.
textEmailAddress 21 Text that will be used as an e-mail address. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_EMAIL_ADDRESS.
textEmailSubject 31 Text that is being supplied as the subject of an e-mail. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_EMAIL_SUBJECT.
textFilter b1 Text that is filtering some other data. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_FILTER.
textImeMultiLine 40001 Can be combined with text and its variations to indicate that though the regular text view should not be multiple lines, the IME should provide multiple lines if it can. Corresponds to TYPE_TEXT_FLAG_IME_MULTI_LINE.
textLongMessage 51 Text that is the content of a long message. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_LONG_MESSAGE.
textMultiLine 20001 Can be combined with text and its variations to allow multiple lines of text in the field. If this flag is not set, the text field will be constrained to a single line. Corresponds to TYPE_TEXT_FLAG_MULTI_LINE.
textNoSuggestions 80001 Can be combined with text and its variations to indicate that the IME should not show any dictionary-based word suggestions. Corresponds to TYPE_TEXT_FLAG_NO_SUGGESTIONS.
textPassword 81 Text that is a password. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD.
textPersonName 61 Text that is the name of a person. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PERSON_NAME.
textPhonetic c1 Text that is for phonetic pronunciation, such as a phonetic name field in a contact entry. Corresponds toTYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PHONETIC.
textPostalAddress 71 Text that is being supplied as a postal mailing address. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_POSTAL_ADDRESS.
textShortMessage 41 Text that is the content of a short message. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_SHORT_MESSAGE.
textUri 11 Text that will be used as a URI. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_URI.
textVisiblePassword 91 Text that is a password that should be visible. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_VISIBLE_PASSWORD.
textWebEditText a1 Text that is being supplied as text in a web form. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_WEB_EDIT_TEXT.
textWebEmailAddress d1 Text that will be used as an e-mail address on a web form. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS.
textWebPassword e1 Text that will be used as a password on a web form. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_WEB_PASSWORD.
time 24 For entering a time. Corresponds to TYPE_CLASS_DATETIME | TYPE_DATETIME_VARIATION_TIME.

指定输入法行动

Most soft input methods provide a user action button in the bottom corner that‘s appropriate for the current text field. By default, the system uses this button for either a Next or Done action unless your text field allows multi-line text (such as with android:inputType="textMultiLine"), in which case the action button is a carriage return. However, you can specify additional actions that might be more appropriate for your text field, such as Send or Go.

To specify the keyboard action button, use the android:imeOptions attribute with an action value such as "actionSend" or "actionSearch". For example:

Figure 4. The Send button appears when you declare android:imeOptions="actionSend".

<EditText
    android:id="@+id/search"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search_hint"
    android:inputType="text"
    android:imeOptions="actionSend" />

You can then listen for presses on the action button by defining a TextView.OnEditorActionListener for the EditText element. In your listener, respond to the appropriate IME action ID defined in the EditorInfo class, such as IME_ACTION_SEND. For example:

EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            sendMessage();
            handled = true;
        }
        return handled;
    }
})
时间: 2024-10-14 04:47:11

Android:日常学习笔记(7)———探究UI开发(1)的相关文章

Android:日常学习笔记(8)———探究UI开发(5)

Android:日常学习笔记(8)---探究UI开发(5) ListView控件的使用 ListView的简单用法 public class MainActivity extends AppCompatActivity { private String[] data={"Apple","Banana","Orange","Watermelon","Pear","Grape","

Android:日常学习笔记(8)———探究UI开发(2)

Android:日常学习笔记(8)---探究UI开发(2) 对话框 说明: 对话框是提示用户作出决定或输入额外信息的小窗口. 对话框不会填充屏幕,通常用于需要用户采取行动才能继续执行的模式事件. 提示: Dialog 类是对话框的基类,但您应该避免直接实例化 Dialog,而是使用下列子类之一: AlertDialog此对话框可显示标题.最多三个按钮.可选择项列表或自定义布局. DatePickerDialog 或 TimePickerDialog此对话框带有允许用户选择日期或时间的预定义 UI

Android:日常学习笔记(7)———探究UI开发(4)

Android:日常学习笔记(7)---探究UI开发(4) UI概述  View 和 ViewGrou Android 应用中的所有用户界面元素都是使用 View 和 ViewGroup 对象构建而成.View 对象用于在屏幕上绘制可供用户交互的内容.ViewGroup 对象用于储存其他 View(和 ViewGroup)对象,以便定义界面的布局. 说明: View是安卓中最基本的一种UI,它可以在屏幕上绘制一块矩形区域,并能响应这块区域的各种事件,我们使用的各种控件都是在View的基础上进行的

Android:日常学习笔记(6)——探究活动(3)

Android:日常学习笔记(6)--探究活动(3) 活动的生命周期 返回栈 Android中的活动是可以叠加的,我们每启动一个新活动,就会覆盖在原来的活动上,点击Back以后销毁最上面的活动,下面的活动就会重新显现出来.Android是使用任务(Task)来管理活动的,一个任务就是一组存放在栈里的活动的集合. 默认情况下,每当我们启动一个新的活动,他会在返回栈中入栈,并处于栈顶位置.而每当我们按下Back或者Finish以后,处于栈顶位置的活动会出栈. 活动的状态 运行状态(栈顶的元素).暂停

Android:日常学习笔记(9)———探究持久化技术

Android:日常学习笔记(9)---探究持久化技术 引入持久化技术 什么是持久化技术 持久化技术就是指将那些内存中的瞬时数据保存到存储设备中,保证即使在手机或电脑关机的情况下,这些数据仍然不会丢失. Android系统提供的三种持久化技术: 文件存储.SharedPreference(使用共享首选项)存储以及数据库存储. 文件存储 说明: 您可以直接在设备的内部存储中保存文件.默认情况下,保存到内部存储的文件是应用的私有文件,其他应用(和用户)不能访问这些文件. 当用户卸载您的应用时,这些文

Android:日常学习笔记(9)———探究广播机制

Android:日常学习笔记(9)---探究广播机制 引入广播机制 Andorid广播机制 广播是任何应用均可接收的消息.系统将针对系统事件(例如:系统启动或设备开始充电时)传递各种广播.通过将 Intent 传递给 sendBroadcast().sendOrderedBroadcast() 或 sendStickyBroadcast(),您可以将广播传递给其他应用. Android提供了一套完整的API,允许应用程序自由地发送和接受广播.发送广播使用Intent,接受广播使用 广播接收器(B

Android:日常学习笔记(10)———使用LitePal操作数据库

Android:日常学习笔记(10)---使用LitePal操作数据库 引入LitePal 什么是LitePal LitePal是一款开源的Android数据库框架,采用了对象关系映射(ORM)的模式,将平时开发时最常用的一些数据库功能进行了封装,使得开发者不用编写一行SQL语句就可以完成各种建表.増删改查的操作.并且LitePal很"轻",jar包大小不到100k,而且近乎零配置,这一点和Hibernate这类的框架有很大区别.目前LitePal的源码已经托管到了GitHub上. 关

Android NDK学习笔记(一) 为什么要用NDK?

NDK是什么 NDK是Native Development Kit的简称,即本地开发工具包.通过NDK,Android允许开发人员使用本地代码语言(例如C/C++)来完成应用的部分(甚至全部)功能.注意:由于翻译原因,有些地方也把Native翻译为"原生". NDK是SDK的一个补充,可以帮助你做这些事情: 生成可以在ARM CPU,Android 1.5(及以上)平台运行的JNI兼容的共享库. 将生成的共享库放置在应用程序项目路径的合适位置,使其能自动地添加进你最终的(和经过签名的)

java/android 设计模式学习笔记(7)---装饰者模式

这篇将会介绍装饰者模式(Decorator Pattern),装饰者模式也称为包装模式(Wrapper Pattern),结构型模式之一,其使用一种对客户端透明的方式来动态的扩展对象的功能,同时它也是继承关系的一种替代方案之一,但比继承更加灵活.在现实生活中也可以看到很多装饰者模式的例子,或者可以大胆的说装饰者模式无处不在,就拿一件东西来说,可以给它披上无数层不一样的外壳,但是这件东西还是这件东西,外壳不过是用来扩展这个东西的功能而已,这就是装饰者模式,装饰者的这个角色也许各不相同但是被装饰的对