Android 自定义EditText settext不设置文本
我正在使用自定义edittext,但问题是我无法为自定义edittext设置文本。以下是我从现有答案中尝试的所有内容 这仍然不起作用。我没有得到任何错误,所以不知道为什么它不起作用 这也不起作用 XML文件Android 自定义EditText settext不设置文本,android,android-edittext,android-custom-view,Android,Android Edittext,Android Custom View,我正在使用自定义edittext,但问题是我无法为自定义edittext设置文本。以下是我从现有答案中尝试的所有内容 这仍然不起作用。我没有得到任何错误,所以不知道为什么它不起作用 这也不起作用 XML文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#FFFFFF"
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" >
<com.random.practiceproject.LinedEditText
android:layout_width="301dp"
android:inputType="text"
android:layout_height="match_parent" />
</LinearLayout>
这是密码
public class LinedEditText extends EditText {
private static Paint linePaint;
static {
linePaint = new Paint();
linePaint.setColor(Color.BLACK);
linePaint.setStyle(Paint.Style.STROKE);
}
public LinedEditText(Context context)
{
super(context);
}
public LinedEditText(Context context, AttributeSet attributes) {
super(context, attributes);
}
@Override
protected void onDraw(Canvas canvas) {
Rect bounds = new Rect();
int firstLineY = getLineBounds(0, bounds);
/*int firstLineY=0;
for(int i =0;i<getLineCount();i++)
{
firstLineY = (i + 1) * getLineHeight();
}*/
int lineHeight = getLineHeight();
int totalLines = Math.max(getLineCount(), getHeight() / lineHeight);
for (int i = 0; i < totalLines; i++) {
int lineY = firstLineY + i * lineHeight;
canvas.drawLine(bounds.left, lineY, bounds.right, lineY, linePaint);
}
super.onDraw(canvas);
}
}
公共类LinedEditText扩展了EditText{
私人静电漆;
静止的{
linePaint=新油漆();
linePaint.setColor(颜色:黑色);
linePaint.setStyle(Paint.Style.STROKE);
}
public linededitext(上下文)
{
超级(上下文);
}
public linededitext(上下文上下文、属性集属性){
超级(上下文、属性);
}
@凌驾
受保护的void onDraw(画布){
Rect bounds=new Rect();
int firstLineY=getLineBounds(0,bounds);
/*int firstLineY=0;
对于(int i=0;i而言,问题在于您在onCreate
中创建了LineEditText
的新实例,并使用它,但不添加到布局中。在布局中还有另一个LineEditText
实例,您不使用
因此,您必须替换:
lt = new LinedEditText(this);
与:
或者您需要通过ViewGroup.addView()
将lt
添加到布局中,但我认为您需要使用第一个变量。简单的et.setText不起作用吗?@abbath也没有尝试过。刚开始键入相同的:)我想,既然我正在实例化,那就足够了。@Weirderd没有。主要的一点是,生命周期开始时的活动对布局一无所知。但你可以通过调用setContentView
来帮助它。你可以传递一个xml布局或你的自定义视图组。然后活动就可以使用它。在你的例子中,它是xml fil使用LinearLayout和继承的LinedItemText创建一个独立的实例。然后,您需要获取您的视图,并通过findViewById
方法使用它们。但是,相反,您创建了一个独立的LinedItemText
@Weirderd…实例之后,您有两个LinedItemText实例。一个从xml布局解析,另一个由您(通过显式调用一个参数构造函数),第一个实例不使用,第二个实例不匹配任何内容(显示它)。实例化只会为您创建新对象。您必须将此新创建的对象添加回布局,或者必须首先在XML中定义它,然后必须执行findviewbyid,然后获取对它的引用,而不是为该对象执行新操作
lt = new LinedEditText(this);
lt = (LinedEditText) findViewById(/*provide your id*/)