Android 使用按钮创建编辑文本
我必须创建内部带有按钮的EditText,并分配按下按钮后将运行的功能。然后我想让它可重用——以某种方式将它作为一个元素添加到我的活动中。我想知道如何扩展EditTextView并在其中添加按钮和创建函数。Android 使用按钮创建编辑文本,android,android-edittext,android-view,Android,Android Edittext,Android View,我必须创建内部带有按钮的EditText,并分配按下按钮后将运行的功能。然后我想让它可重用——以某种方式将它作为一个元素添加到我的活动中。我想知道如何扩展EditTextView并在其中添加按钮和创建函数。 有任何建议/教程吗?您需要创建自定义视图组件 下面的链接有示例,将解决您的问题 您可以扩展LinearLayout或RelativeLayout,并在构造函数中添加按钮和编辑文本。但是,您需要使用getter方法来获取EditText或按钮。这里有一个例子。我还没有测试过,但它会给你一个想
有任何建议/教程吗?您需要创建自定义视图组件 下面的链接有示例,将解决您的问题
您可以扩展LinearLayout或RelativeLayout,并在构造函数中添加按钮和编辑文本。但是,您需要使用getter方法来获取EditText或按钮。这里有一个例子。我还没有测试过,但它会给你一个想法。请记住,如果要使用Button或EditText的xml属性,应该在attr.xml中定义它们,然后使用TypedArray获取它们。由于对象是LinearLayout,如果不在attr.xml中定义EditText或Button属性,则无法使用它们
public class CustomEditText extends LinearLayout {
private AppCompatButton mButton;
private AppCompatEditText mEditText;
public CustomEditText(Context context) {
this(context, null, 0);
}
public CustomEditText(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOrientation(HORIZONTAL);
/** Get Attributes **/
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomEditText);
int lines = typedArray.getInt(R.styleable.CustomEditText_android_lines, 1);
int gravity = typedArray.getInt(R.styleable.CustomEditText_android_gravity, Gravity.CENTER_VERTICAL);
int inputType = typedArray.getInt(R.styleable.CustomEditText_android_inputType, InputType.TYPE_CLASS_TEXT);
int imeOptions = typedArray.getInt(R.styleable.CustomEditText_android_imeOptions, EditorInfo.IME_ACTION_DONE);
String hint = typedArray.getString(R.styleable.CustomEditText_hint);
typedArray.recycle();
/** Construct Button **/
mButton = new AppCompatButton(context);
addView(mButton);
/** Construct EditText **/
mEditText = new AppCompatEditText(context);
mEditText.setGravity(gravity);
mEditText.setHint(hint);
mEditText.setImeOptions(imeOptions | EditorInfo.IME_FLAG_NO_EXTRACT_UI);
mEditText.setInputType(inputType | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
if (!((inputType & InputType.TYPE_TEXT_FLAG_MULTI_LINE) == InputType.TYPE_TEXT_FLAG_MULTI_LINE
&& lines == 1)) mEditText.setLines(lines);
addView(mEditText);
}
public AppCompatEditText getEditText() {
return mEditText;
}
public AppCompatButton getButton() {
return mButton;
}
}
这是您需要做的:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionHint="yourhint"
android:dropDownHeight="match_parent"
android:hint="From"
android:padding="20dp"
android:visibility="visible"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@id/image"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
</LinearLayout>
如果你想在编辑文本中添加一些图标或按钮,你可以这样做
public class MKEditText extends AppCompatEditText {
public interface IconClickListener {
public void onClick();
}
private IconClickListener mIconClickListener;
private static final String TAG = MKEditText.class.getSimpleName();
private final int EXTRA_TOUCH_AREA = 50;
private Drawable mDrawable;
private boolean touchDown;
public MKEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MKEditText(Context context) {
super(context);
}
public MKEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void showRightIcon() {
mDrawable = ContextCompat.getDrawable(getContext(), R.drawable.ic_android_black_24dp);
setIcon();
}
public void setIconClickListener(IconClickListener iconClickListener) {
mIconClickListener = iconClickListener;
}
private void setIcon() {
Drawable[] drawables = getCompoundDrawables();
setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], mDrawable, drawables[3]);
setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
setSelection(getText().length());
}
@Override
public boolean onTouchEvent(MotionEvent event) {
final int right = getRight();
final int drawableSize = getCompoundPaddingRight();
final int x = (int) event.getX();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (x + EXTRA_TOUCH_AREA >= right - drawableSize && x <= right + EXTRA_TOUCH_AREA) {
touchDown = true;
return true;
}
break;
case MotionEvent.ACTION_UP:
if (x + EXTRA_TOUCH_AREA >= right - drawableSize && x <= right + EXTRA_TOUCH_AREA && touchDown) {
touchDown = false;
if (mIconClickListener != null) {
mIconClickListener.onClick();
}
return true;
}
touchDown = false;
break;
}
return super.onTouchEvent(event);
}
}
MKEditText mkEditText = (MKEditText) findViewById(R.id.password);
mkEditText.showRightIcon();
mkEditText.setIconClickListener(new MKEditText.IconClickListener() {
@Override
public void onClick() {
// You can do action here. ex you can start activity here like this
startActivityForResult(new Intent(MainActivity.this, SampleActivity.class), 1);
}
});
注意:在本文中,我使用的是SetCompoundDrawableSwithinInstructBounds,
因此,如果您想更改图标位置,可以使用
SetCompoundDrawableSwithinInstincBounds在setIcon中
创建一些像这样的自定义文本
public class MKEditText extends AppCompatEditText {
public interface IconClickListener {
public void onClick();
}
private IconClickListener mIconClickListener;
private static final String TAG = MKEditText.class.getSimpleName();
private final int EXTRA_TOUCH_AREA = 50;
private Drawable mDrawable;
private boolean touchDown;
public MKEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MKEditText(Context context) {
super(context);
}
public MKEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void showRightIcon() {
mDrawable = ContextCompat.getDrawable(getContext(), R.drawable.ic_android_black_24dp);
setIcon();
}
public void setIconClickListener(IconClickListener iconClickListener) {
mIconClickListener = iconClickListener;
}
private void setIcon() {
Drawable[] drawables = getCompoundDrawables();
setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], mDrawable, drawables[3]);
setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
setSelection(getText().length());
}
@Override
public boolean onTouchEvent(MotionEvent event) {
final int right = getRight();
final int drawableSize = getCompoundPaddingRight();
final int x = (int) event.getX();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (x + EXTRA_TOUCH_AREA >= right - drawableSize && x <= right + EXTRA_TOUCH_AREA) {
touchDown = true;
return true;
}
break;
case MotionEvent.ACTION_UP:
if (x + EXTRA_TOUCH_AREA >= right - drawableSize && x <= right + EXTRA_TOUCH_AREA && touchDown) {
touchDown = false;
if (mIconClickListener != null) {
mIconClickListener.onClick();
}
return true;
}
touchDown = false;
break;
}
return super.onTouchEvent(event);
}
}
MKEditText mkEditText = (MKEditText) findViewById(R.id.password);
mkEditText.showRightIcon();
mkEditText.setIconClickListener(new MKEditText.IconClickListener() {
@Override
public void onClick() {
// You can do action here. ex you can start activity here like this
startActivityForResult(new Intent(MainActivity.this, SampleActivity.class), 1);
}
});
发布您所做的一切。您好,您想让密码可见或隐藏,或者做一些操作吗?您好,请检查我的答案,我举了一个例子,在编辑文本中放置一些图标,使其可单击。@coddder123查看示例,如果有任何不清楚的地方,请告诉我。我如何调用函数startActivityForResult?我这里有个错误。ShouldShowRequestPermissionRegulation也不起作用。您想在onClick..内调用startActivityForResult?那么您需要在该活动中获得一些权限。不,在我的自定义函数内。我想通过按钮获取edittext,并在按下按钮并检查时间权限后进入其他活动。您的答案是可以的,但我想知道这些函数应该放在哪里?例如,创建自定义edittext类,然后向其添加功能?您可以在onClick()内执行此操作