直接贴上代码吧: <LinearLayout 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" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <include android:id="@+id/clearableEditText" android:layout_width="match_parent" android:layout_height="wrap_content" layout="@layout/clearable_edit_text" /> </LinearLayout> clearable_edit_text.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="wrap_content" android:orientation="vertical" > <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/clearButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/clear" /> </LinearLayout> ClearableEditText.java package com.hyc.compoundviews; import com.hyc.compoundviewdemo.R; 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 ClearableEditText extends LinearLayout { // Context context = null; EditText editText = null; Button clearBut = null; public ClearableEditText(Context context) { // this.context = context; super(context); // TODO Auto-generated constructor stub LayoutInflater layoutInflater = (LayoutInflater) getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); layoutInflater.inflate(R.layout.clearable_edit_text, this, true); editText = (EditText) findViewById(R.id.editText); clearBut = (Button) findViewById(R.id.clearButton); hookupButton(); } public ClearableEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public ClearableEditText(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } private void hookupButton() { clearBut.setOnClickListener(new ClearButOnClickListenerImpl()); } private class ClearButOnClickListenerImpl implements OnClickListener { @Override public void onClick(View arg0) { editText.setText(""); } } } 再加上一个: <string name="clear">Clear</string>
|
|
没有填完下面的表单项就按回车提交了这帖子
|
|
35分 |
改成这样试试
public ClearableEditText(Context context) { // this.context = context; super(context); // TODO Auto-generated constructor stub init(); } public ClearableEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub init(); } public ClearableEditText(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub init(); } private void hookupButton() { clearBut.setOnClickListener(new ClearButOnClickListenerImpl()); } private class ClearButOnClickListenerImpl implements OnClickListener { @Override public void onClick(View arg0) { editText.setText(""); } } private void init(){ LayoutInflater layoutInflater = (LayoutInflater) getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); layoutInflater.inflate(R.layout.clearable_edit_text, this, true); editText = (EditText) findViewById(R.id.editText); clearBut = (Button) findViewById(R.id.clearButton); hookupButton(); } |
5分 |
debug看看有没执行到editText.setText(“”);
|
果然没有跳进去 |
|
OK了。Thank U! |