①自定义复合控件的布局:

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 阅读(958) 评论(0)  编辑  收藏 所属分类: Android

只有注册用户登录后才能发表评论。


网站导航: