閱讀384 返回首頁    go 汽車大全


API Demos 2.3 學習筆記 (5)-- Text->LogTextBox

更多精彩內容,請點擊閱讀:《API Demos 2.3 學習筆記》


 Android中對於Button控件的監聽方法主要有兩種:

1、設置監聽器

通過設置監聽器來監聽用戶對於按鈕的點擊響應。當用戶點擊該按鈕時,便會觸發監聽器,並執行監聽器中onClick方法內部定義的指定動作。

         final Button button = (Button) findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // 點擊觸發時需要執行的動作
             }
         });

2、自定義監聽方法
首先需要在Layout布局文件中為該按鈕添加屬性(android:onClick="selfDestruct" )。其中selfDestruct為自定義監聽方法名稱,後麵需要用到。

	<Button
    		 android:layout_height="wrap_content"
     		android:layout_width="wrap_content"
     		android:text="@string/self_destruct"
     		android:onClick="selfDestruct" />

接著,在Activity中實現自定義監聽方法:

 	public void selfDestruct(View view) {
 	    // 點擊觸發時需要執行的動作
	 }

注: 1、該自定義方法必須是Public類型;

2、該自定義方法必須並且隻能接受一個參數View

下麵我們進行實例代碼解析:

res-value-string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="log_text_box_1_do_nothing_text">Do nothing</string>
    <string name="log_text_box_1_add_text">Add</string>
</resources>

res-layout-log_text_box_1.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:andro 
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- android:onClick="selfDestruct" 監聽方法二需要添加此屬性,其中selfDestruct為自定義監聽方法名稱-->
    <Button
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/log_text_box_1_add_text"
        />

	<!--自定義控件 LogTextBox ,繼承自 TextView -->
    <com.example.android.apis.text.LogTextBox
        android:
        android:background="@drawable/box"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:scrollbars="vertical"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/log_text_box_1_do_nothing_text"/>

</LinearLayout>

src-com.example.android.apis.text-LogTextBox.java

package com.example.android.apis.text;

import android.widget.TextView;
import android.content.Context;
import android.text.method.ScrollingMovementMethod;
import android.text.method.MovementMethod;
import android.text.Editable;
import android.util.AttributeSet;

/**
 * 這是一個可以編輯並且默認可以滾動的TextView控件。
 * 類似缺少光標的EditText控件。
 *
 * <p>
 * <b>XML attributes</b>
 * <p>
 * See
 * {@link android.R.styleable#TextView TextView Attributes},
 * {@link android.R.styleable#View View Attributes}
 */
public class LogTextBox extends TextView {
    public LogTextBox(Context context) {
        this(context, null);
    }

    public LogTextBox(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.textViewStyle);
    }

    public LogTextBox(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @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);
    }
}

src-com.example.android.apis.text-LogTextBox1.java

package com.example.android.apis.text;

import com.example.android.apis.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class LogTextBox1 extends Activity {
    
    private LogTextBox mText;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.log_text_box_1);
        
        mText = (LogTextBox) findViewById(R.id.text);
        
        //對Button的監聽方法一:設置監聽器
        Button addButton = (Button) findViewById(R.id.add);
        addButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                mText.append("This is a test\n");
            } });
        
        
    }
    
    //對Button的監聽方法二:自定義監聽方法,需要設置android:onClick屬性
    //這個方法必須是Public類型的,並且隻能接受view作為唯一參數 
    public void selfDestruct(View view) {
        mText.append("This is a test\n");
    }
}

知識點1android:text="@string/log_text_box_1_add_text"

在android中,建議將所有固定字符串資源放在res/values/string.xml文件中,方便統一管理。在layout布局文件等xml類型的文件中引用字符串資源時,通過"@string/字符串資源名稱"格式進行引用。而Java代碼中需要引用字符串資源時,則通過格式”R.string.字符串資源名“進行引用,具體引用方法如下所示:

//通過string字符串資源獲得CharSequence
CharSequencecs = getText(R.string.log_text_box_1_add_text);
//獲取字符串資源,並且顯示在TextView控件上
mText= (TextView) findViewById(R.id.text);
mText.setText(R.string.log_text_box_1_add_text);

知識點2android:scrollbars="vertical"

該屬性定義控件在滾動時是否顯示滾動條。該屬性可以同時取多個值,但必須用”|”隔開。例如:橫向縱向都支持滾動條顯示(android:scrollbars="horizontal|vertical"

常量

描述

none

0x00000000

不顯示滾動條

horizontal

0x00000100

僅僅現實橫向滾動條

vertical

0x00000200

僅僅現實縱向滾動條


知識點3android:layout_weight="1"

layout_weight用於給一個線性布局中的諸多視圖的重要度賦值。所有的視圖都有一個layout_weight值,默認為零,意思是需要顯示多大的視圖就占據多大的屏幕空間。若賦一個高於零的值,則將父視圖中的可用空間分割,分割大小具體取決於每一個視圖的layout_weight值以及該值在當前屏幕布局的整體layout_weight值和在其它視圖屏幕布局的layout_weight值中所占的比率而定。

舉個例子:比如說我們在水平方向上有一個文本標簽和兩個文本編輯元素。該文本標簽並無指定layout_weight值,所以它將占據需要提供的最少空間。如果兩個文本編輯元素每一個的layout_weight值都設置為1,則兩者平分在父視圖布局剩餘的寬度(因為我們聲明這兩者的重要度相等)。如果兩個文本編輯元素其中第一個的layout_weight值設置為1,而第二個的設置為2,則剩餘空間的三分之一分給第一個,三分之而分給第二個(數值越大,重要度越高)

關於layout_weight更完整的解釋,請參考以下文章:https://blog.csdn.net/jincf2011/article/details/6598256

注:值得注意的是,在水平布局中設置layout_weight的時候,必須這樣進行設置android:layout_width="0dip"。同理,在垂直布局中設置layout_weight時,也必須要做相應設置android:layout_height="0dip"


效果預覽:





最後更新:2017-04-02 06:51:56

  上一篇:go Spring框架開發實踐--視頻
  下一篇:go API Demos 2.3 學習筆記 (4)-- Text-&gt;Linkify