在Android開發中,我們的Activity都不免要用到很多的View組件,而對於每一個View我們都要findViewById,設定監聽器,擷取使用者輸入的結果等操作。如果我們細心想想,這些瑣碎的操作是不是跟這個Activity的處理邏輯沒有很大的關係呢?很多的Activity中都要用到一些常見的View組合,能不能把他們抽象出來形成一種可以複用的複合組件呢?
這篇文章就是基於這種想法,開發了一個簡單的組件,對於Android新手來說,可以作為一種參考吧。
EditTextWithLabel組件類定義:(EditTextWithLabel.java)
| 代碼如下 |
複製代碼 |
package com.raysmond.component; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; public class EditTextWithLabel extends LinearLayout{ TextView label; EditText text; public EditTextWithLabel(Context context,AttributeSet attrs){ super(context, attrs); setOrientation(HORIZONTAL); LayoutInflater.from(context).inflate(R.layout.edit_text_with_label, this, true); label = (TextView) findViewById(R.id.comp_le_label); text = (EditText) findViewById(R.id.comp_le_content); } public boolean setSubViewFocus(){ return text.requestFocus(); } public EditText getEditText(){ return text; } public String getContent(){ return text.getText().toString(); } public void setContent(String str){ text.setText(str); } public void setError(String error){ text.setError(error); } } |
EditTextWithLabel組件的布局xml(edit_text_with_label.xml)
| 代碼如下 |
複製代碼 |
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:id="@+id/comp_le_label" android:layout_width="wrap_content" android:layout_weight="3" android:layout_height="wrap_content" android:paddingRight="5dp" android:text="Label" android:textSize="25sp" android:gravity="center"/> <EditText android:inputType="text" android:id="@+id/comp_le_content" android:layout_width="wrap_content" android:layout_weight="7" android:layout_height="wrap_content" android:textSize="25sp" > <requestFocus /> </EditText> </merge> |
使用EditTextWithLabel複合組件,主Activity的XML (activity_main.xml)
| 代碼如下 |
複製代碼 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:id="@+id/hello_world" android:layout_height="wrap_content" android:textSize="28sp" android:text="帶Label的輸入框組件" /> <com.raysmond.component.EditTextWithLabel android:id="@+id/test_component" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/hello_world" /> </RelativeLayout> |
使用EditTextWithLabel複合組件,主Activity類(MainActivity.java)
| 代碼如下 |
複製代碼 |
package com.raysmond.activity; import com.raysmond.component.EditTextWithLabel; import com.raysmond.component.R; import android.os.Bundle; import android.app.Activity; public class MainActivity extends Activity { EditTextWithLabel component; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findView(); } public void findView(){ component = (EditTextWithLabel) findViewById(R.id.test_component); component.setSubViewFocus(); } } |
Demo: