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

          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 閱讀(976) 評論(0)  編輯  收藏 所屬分類: Android
          主站蜘蛛池模板: 民勤县| 大化| 府谷县| 灵川县| 新民市| 咸宁市| 蒲江县| 新闻| 台州市| 崇阳县| 永清县| 巫溪县| 横山县| 华阴市| 河西区| 尼木县| 民勤县| 织金县| 綦江县| 乌兰浩特市| 攀枝花市| 惠州市| 华安县| 昌图县| 屯门区| 页游| 雅江县| 溧阳市| 山阳县| 舒兰市| 林州市| 奉节县| 前郭尔| 都匀市| 白银市| 临泉县| 江永县| 新郑市| 泌阳县| 应城市| 桃江县|