自定义控件包括一下几步:自定义布局文件(myLinearLayout.xml,实现自定义控件的内部组成) ,继承View(导入自定义的布局文件,设置需要的方法,在代码中可以修改自定义控件的相关属性和动作,重写构造函数、onDraw,(onMeasure)等函数),在布局中加入自己定义的控件(需指定包名),在activity中设置其内容和属性。
1.自定义布局文件myLinearLayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SelfViewActivity" >
<EditText
android:id="@+id/search_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/ib1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:hint="请输入关键字"
android:layout_alignParentTop="true"
android:ems="10" >
<requestFocus />
</EditText>
<ImageButton
android:id="@+id/ib1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:visibility="visible"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/ib2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@id/ib1"
android:visibility="gone"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
2.继承View,导入自定义的布局文件 SearchET.java
public class SearchET extends LinearLayout implements EdtInterface {
ImageButton ib1, ib2;
EditText et;
public SearchET(Context context) {
// TODO Auto-generated constructor stub
super(context);
}
public SearchET(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.myLinearLayout, this,true);
init();
}
private void init() {
ib1 = (ImageButton) findViewById(R.id.ib1);
ib2 = (ImageButton) findViewById(R.id.ib2);
et = (EditText) findViewById(R.id.search_et);
et.addTextChangedListener(watcher);// 为输入框绑定一个监听文字变化的监听器
// 添加按钮点击事件
ib2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
hideBtn();// 隐藏按钮
et.setText("");// 设置输入框内容为空
}
});
ib1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) { }
});
}
TextWatcher watcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) { }
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) { }
@Override
public void afterTextChanged(Editable s) {
if (s.length() == 0) hideBtn();
else showBtn();
}
};
@Override
public void hideBtn() {
// 设置按钮不可见
if (ib2.isShown())
ib2.setVisibility(View.GONE);
}
@Override
public void showBtn() {
// 设置按钮可见
if (!ib2.isShown())
ib2.setVisibility(View.VISIBLE);
}
/**
* 设置1和2的图片资源
*/
public void setImageResource(int resId1, int resId2) {
ib1.setImageResource(resId1);
ib2.setImageResource(resId2);
}
/**
* 设置ib1的点击事件
*/
public void setImageCL(OnClickListener l) {
ib1.setOnClickListener(l);
}
}
interface EdtInterface {
public void hideBtn();
public void showBtn();
}
3.在布局中加入自己定义的控件(需指定包名) testselfview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.selfview.SearchET
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/selfview1"/>
<com.example.selfview.SearchET
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/selfview2"/>
</LinearLayout>
4.在activity中设置其内容和属性
public class SelfViewActivity extends Activity {
SearchET set1,set2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testselfview);
set1 = (SearchET) findViewById(R.id.selfview1);
set2 = (SearchET) findViewById(R.id.selfview2);
set1.setImageCL(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
MyDialog myDialog = new MyDialog(SelfViewActivity.this, R.style.MyDialog);
myDialog.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_self_view, menu);
return true;
}
}