①自定義復(fù)合控件的布局:

          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>

          很簡單,不廢話

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

          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 閱讀(967) 評論(0)  編輯  收藏 所屬分類: Android
          主站蜘蛛池模板: 广南县| 台湾省| 井陉县| 疏勒县| 连平县| 信阳市| 石阡县| 萍乡市| 宝应县| 青龙| 巫溪县| 西充县| 玉环县| 彩票| 丘北县| 抚顺县| 海原县| 莱阳市| 天祝| 满洲里市| 萨迦县| 额尔古纳市| 白水县| 新沂市| 保靖县| 博白县| 嘉峪关市| 英吉沙县| 铁岭县| 阿拉善左旗| 乌兰浩特市| 金寨县| 乐至县| 英吉沙县| 皋兰县| 平昌县| 上杭县| 天长市| 邢台市| 余江县| 乌审旗|