Android ApiDemos示例解析(93):Text->LogTextBox

Android中TextView用于显示文字串并可以设置成“可编辑”。事实上TextView 是个功能完全的编辑框,但缺省设置成只读(相当于Windows中的label) ,它的子类EditText 打开了编辑功能允许用户编辑文本。

本例从TextView 派生类一个子类 LogTextBox ,其功能可以用于显示应用日志,提供了垂直滚动条(TextView缺省不带滚动条也不响应用户按键)。

它重载了几个方法,可以试着将这些方法注释掉,看看TextView的缺省行为。

@Override
protected MovementMethod getDefaultMovementMethod() {
 return ScrollingMovementMethod.getInstance();
}
 
@Override
public Editable getText() {
 return (Editable) super.getText();
}
 
@Override
public void setText(CharSequence text, BufferType type) {
 super.setText(text, BufferType.EDITABLE);
}

其中setText 将该文本框编辑功能打开,允许使用输入框。

Android ApiDemos示例解析(93):Text->LogTextBox_第1张图片使用滚动条,可以在Layout中设置android:scrollbars的类型,本例使用垂直滚动条。

<com.example.android.apis.text.LogTextBox
android:id=”@+id/text”
android:background=”@drawable/box”
android:layout_width=”match_parent”
android:layout_height=”0dip”
android:layout_weight=”1″
android:scrollbars=”vertical“/>

TextView缺省情况是不响应用户操作的,因此如果TextView能够使用滚动条,需要通知TextView使用滚动条的方式来响应用户操作,这就是getDefaultMovementMethod的目的

Android ApiDemos示例解析(93):Text->LogTextBox_第2张图片

TextView的子类很多,包括各种按钮如Button,RadioButton等,编辑框EditText 等。TextView提供的功能非常强大,具体可以参见Android TextView文档。

 

你可能感兴趣的:(android,windows,layout,文档,button,RadioButton)