记录自定义EditTextView 遇到的问题
效果
public class MyEditText
extends android.support.v7.widget.AppCompatEditText {
private Paint paint;
private RectF rect;
private float radiou;
private Path mpath;
public MyEditText(Context context) {
this(context, null);
}
public MyEditText(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MyEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyEditText);
float riangle_width = typedArray.getDimension(R.styleable.MyEditText_etriangle_width, context.getResources().getDimension(R.dimen.dp_20));
float riangle_height = typedArray.getDimension(R.styleable.MyEditText_etriangle_height, context.getResources().getDimension(R.dimen.dp_20));
float riangle_margin = typedArray.getDimension(R.styleable.MyEditText_etriangle_margin, context.getResources().getDimension(R.dimen.dp_20));
radiou = typedArray.getDimension(R.styleable.MyEditText_etradiou, context.getResources().getDimension(R.dimen.dp_10));
typedArray.recycle();
paint = new Paint();
paint.setColor(context.getResources().getColor(R.color.text_bg));
paint.setAntiAlias(true);
mpath = new Path();
//mpathd的起始位置
mpath.moveTo(riangle_margin, riangle_height);
mpath.lineTo(riangle_margin + riangle_width / 2, 0);
mpath.lineTo(riangle_margin + riangle_width, riangle_height);
//将mpath封闭,
mpath.close();
rect = new RectF();
rect.top = riangle_height;
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(mpath, paint);
rect.set(0, rect.top, getWidth(), getHeight());
canvas.drawRoundRect(rect, radiou, radiou, paint);
super.onDraw(canvas);
}
}
属性
<declare-styleable name="MyEditText">
<attr name="etriangle_width" format="dimension" />
<attr name="etriangle_height" format="dimension" />
<attr name="etriangle_margin" format="dimension" />
<attr name="etradiou" format="dimension" />
</declare-styleable>
xml布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".view.activity.IdeaActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:navigationIcon="@drawable/ic_back" />
<TextView
android:id="@+id/title"
style="@style/text_style_black_title"
android:layout_marginStart="20dp"
android:text="@string/issue_return" />
<com.qybm.zhyp.view.MyEditText
android:id="@+id/idea_et"
android:focusableInTouchMode="true"
style="@style/match_style"
android:layout_margin="20dp"
android:paddingStart="15dp"
android:minWidth="@dimen/dp_20"
android:paddingTop="25dp"
android:paddingEnd="15dp"
android:paddingBottom="20dp"
android:hint="@string/issue_return"
android:minLines="8"
android:textColor="@android:color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/intro"
app:riangle_height="10dp"
app:riangle_margin="20dp"
app:riangle_width="20dp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/idea_imgs"
style="@style/match_style"
android:layout_gravity="center"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:orientation="vertical"
app:layoutManager="android.support.v7.widget.GridLayoutManager"
app:spanCount="4" />
<Button
android:id="@+id/idea_submit"
style="@style/match_style"
android:layout_margin="15dp"
android:background="@drawable/red_pay_button_bg"
android:text="@string/submit" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
问题
点击无法弹出键盘
尝试
requestFocus() 无效
android:focusable="true"无效
outouch+inputmethodmanager showInputsoft 无效
解决
android:focusableInTouchMode="true"