①自定義復合控件的布局:

          clearable_edit_text.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/editText"
                android:layout_width 
          = "200dp"
                android:layout_height 
          = "wrap_content"
                
          />
            
          <Button
                
          android:id = "@+id/clearButton"
                android:layout_width 
          = "100dp"
                android:layout_height 
          = "wrap_content"
                android:text 
          = "Clear"
                
          />
          </LinearLayout>

          很簡單,不廢話

          ②選擇適合放置子控件的布局類,并將其擴展

          package com.yinger;

          import android.content.Context;
          import android.util.AttributeSet;
          import android.view.LayoutInflater;
          import android.view.View;
          import android.widget.Button;
          import android.widget.EditText;
          import android.widget.LinearLayout;

          public class MyClearableEditText extends LinearLayout {

              EditText editText;
              Button clearButton;

              
          public MyClearableEditText(Context context, AttributeSet attrs) {
                  
          super(context, attrs);
                  
          // TODO Auto-generated constructor stub
                  init();
                  hookupButton();
              }

              
          public MyClearableEditText(Context context) {
                  
          super(context);
                  init();
                  hookupButton();
              }

              
          private void init() {
                  String infService 
          = Context.LAYOUT_INFLATER_SERVICE;
                  LayoutInflater li;
                  li 
          = (LayoutInflater) getContext().getSystemService(infService);
                  li.inflate(R.layout.clearable_edit_text, 
          thistrue);
                  editText 
          = (EditText) findViewById(R.id.editText);
                  clearButton 
          = (Button) findViewById(R.id.clearButton);
              }

          //    private void init2() {
          //        setOrientation(LinearLayout.VERTICAL);
          //        editText = new EditText(getContext());
          //        clearButton = new Button(getContext());
          //        clearButton.setText("Clear");
          //        int lHeight = LayoutParams.WRAP_CONTENT;
          //        int lWidth = LayoutParams.FILL_PARENT;
          //        
          //        addView(editText,new LinearLayout.LayoutParams(lWidth,lHeight));
          //        addView(clearButton,new LinearLayout.LayoutParams(lWidth,lHeight));
          //    }
              
              
          private void hookupButton(){
                  clearButton.setOnClickListener(
          new Button.OnClickListener(){

                      
          public void onClick(View v) {
                          
          // TODO Auto-generated method stub
                          editText.setText("");
                      }
                  });
              }
          }

          ③引用

          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation
          ="vertical"
              android:layout_width
          ="fill_parent"
              android:layout_height
          ="fill_parent"
              
          >
          <TextView  
              
          android:layout_width="fill_parent" 
              android:layout_height
          ="wrap_content" 
              android:text
          ="@string/hello"
              
          />
          <com.yinger.MyClearableEditText
              android:layout_width
          ="fill_parent" 
              android:layout_height
          ="wrap_content" 
          />
          </LinearLayout>



          posted on 2011-06-12 09:53 Ying-er 閱讀(977) 評論(0)  編輯  收藏 所屬分類: Android
          主站蜘蛛池模板: 巴塘县| 乐都县| 南江县| 南澳县| 青州市| 射洪县| 晋江市| 县级市| 来安县| 罗源县| 合江县| 庄浪县| 密山市| 婺源县| 泰州市| 罗源县| 兴山县| 长沙县| 彭山县| 湖南省| 江西省| 翼城县| 井陉县| 色达县| 济宁市| 石狮市| 桃园县| 海口市| 延寿县| 和平区| 正宁县| 常山县| 舟曲县| 五河县| 柯坪县| 陵川县| 孟州市| 丰顺县| 松潘县| 永登县| 教育|