EditText 中的图像按钮
ImageButton in EditText
使 图像按钮 保持在 EditText 右上角的代码是什么,即使滚动而不是跟随数据输入导致外观混乱?
我会创建一个自定义组件,扩展 RelativeLayout
,然后应用自定义布局。
看看这篇文章,只需对这段代码进行少量编辑即可获得所需的结果:
https://arunbadole1209.wordpress.com/2011/12/16/how-to-create-edittext-with-crossx-button-at-end-of-it/
clearable_edit_text.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/clearable_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingRight="35dip"
/>
<Button
android:id="@+id/clearable_button_clear"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentRight="true"
android:background="@drawable/image_clear"
android:layout_centerVertical="true"
android:layout_marginRight="5dip"/>
</RelativeLayout>
ClearableEditText.java
public class ClearableEditText extends RelativeLayout
{
LayoutInflater inflater = null;
EditText edit_text;
ImageButton btn_clear;
public ClearableEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
initViews();
}
public ClearableEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
initViews();
}
public ClearableEditText(Context context)
{
super(context);
// TODO Auto-generated constructor stub
initViews();
}
void initViews()
{
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.clearable_edit_text, this, true);
edit_text = (EditText) findViewById(R.id.clearable_edit);
btn_clear = (ImageButton) findViewById(R.id.clearable_button_clear);
btn_clear.setVisibility(RelativeLayout.INVISIBLE);
clearText();
showHideClearButton();
}
void clearText()
{
btn_clear.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
edit_text.setText("");
}
});
}
void showHideClearButton()
{
edit_text.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// TODO Auto-generated method stub
if (s.length() > 0)
btn_clear.setVisibility(RelativeLayout.VISIBLE);
else
btn_clear.setVisibility(RelativeLayout.INVISIBLE);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s)
{
// TODO Auto-generated method stub
}
});
}
public Editable getText()
{
Editable text = edit_text.getText();
return text;
}
}
要使用它,声明:
<com.yourpackage.ClearableEditText
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
使 图像按钮 保持在 EditText 右上角的代码是什么,即使滚动而不是跟随数据输入导致外观混乱?
我会创建一个自定义组件,扩展 RelativeLayout
,然后应用自定义布局。
看看这篇文章,只需对这段代码进行少量编辑即可获得所需的结果: https://arunbadole1209.wordpress.com/2011/12/16/how-to-create-edittext-with-crossx-button-at-end-of-it/
clearable_edit_text.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/clearable_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingRight="35dip"
/>
<Button
android:id="@+id/clearable_button_clear"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentRight="true"
android:background="@drawable/image_clear"
android:layout_centerVertical="true"
android:layout_marginRight="5dip"/>
</RelativeLayout>
ClearableEditText.java
public class ClearableEditText extends RelativeLayout
{
LayoutInflater inflater = null;
EditText edit_text;
ImageButton btn_clear;
public ClearableEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
initViews();
}
public ClearableEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
initViews();
}
public ClearableEditText(Context context)
{
super(context);
// TODO Auto-generated constructor stub
initViews();
}
void initViews()
{
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.clearable_edit_text, this, true);
edit_text = (EditText) findViewById(R.id.clearable_edit);
btn_clear = (ImageButton) findViewById(R.id.clearable_button_clear);
btn_clear.setVisibility(RelativeLayout.INVISIBLE);
clearText();
showHideClearButton();
}
void clearText()
{
btn_clear.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
edit_text.setText("");
}
});
}
void showHideClearButton()
{
edit_text.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// TODO Auto-generated method stub
if (s.length() > 0)
btn_clear.setVisibility(RelativeLayout.VISIBLE);
else
btn_clear.setVisibility(RelativeLayout.INVISIBLE);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s)
{
// TODO Auto-generated method stub
}
});
}
public Editable getText()
{
Editable text = edit_text.getText();
return text;
}
}
要使用它,声明:
<com.yourpackage.ClearableEditText
android:layout_width="fill_parent"
android:layout_height="wrap_content" />