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

          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 閱讀(967) 評論(0)  編輯  收藏 所屬分類: Android
          主站蜘蛛池模板: 隆尧县| 濮阳市| 无锡市| 额尔古纳市| 门头沟区| 高雄市| 郑州市| 安化县| 东阳市| 金湖县| 台安县| 合肥市| 南皮县| 读书| 定西市| 永平县| 封丘县| 页游| 民权县| 巨鹿县| 益阳市| 介休市| 保康县| 页游| 富锦市| 方城县| 昌吉市| 手游| 龙井市| 隆回县| 松滋市| 梁河县| 乃东县| 墨玉县| 铜川市| 原阳县| 濮阳市| 邹城市| 辉南县| 民勤县| 福州市|