java 在 Android 中扩展 EditText。我究竟做错了什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7648924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extending a EditText in Android. What am I doing wrong?
提问by taralex
So I'm trying to get a grasp of using custom controls in Android. But my app crashes on trying to create the activity. Here's the code:
所以我试图掌握在 Android 中使用自定义控件的方法。但是我的应用程序在尝试创建活动时崩溃。这是代码:
package com.myApp;
import android.content.Context;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnClickListener;
public class MyEditText extends EditText implements OnClickListener {
public MyEditText(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public void FlashBorder()
{
//do some custom action
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText txt = (EditText) v;
txt.selectAll();
}
}
Here's the layout xml:
这是布局xml:
<com.myApp.MyEditText
android:id="@+id/edtTaskName"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
回答by Fernando Gallego
You will need to implement these constructors:
您将需要实现这些构造函数:
public class TestEditText extends EditText {
public TestEditText(Context context) {
super(context);
}
public TestEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TestEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public TestEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
}
for example try to do the following :
例如尝试执行以下操作:
public TestEditText(Context context, AttributeSet attrs) {
super(context, attrs);
Log.i("attribute name at index 0", attrs.getAttributeName(0));
}
you will get the following in your logcat :
您将在 logcat 中获得以下内容:
attribute name at index 0 = id
so to deliver these XML attributes to the Super class (EditText) you have to override these constructors.
所以要将这些 XML 属性传递给 Super 类 (EditText),您必须覆盖这些构造函数。
Hope that Helps.
希望有帮助。
回答by Lalit Poptani
You have to add this constructor for creating any custom View.
您必须添加此构造函数来创建任何自定义视图。
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
.....
}
instead of
代替
public MyEditText(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
回答by Amol Suryawanshi
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.os.Build;
import android.util.AttributeSet;
/**
* Created by rohann on 14/07/2016.
*/
public class LightEditText extends android.widget.EditText{
public LightEditText(Context context) {
super(context);
setFont();
}
public LightEditText(Context context, AttributeSet attrs) {
super(context, attrs);
setFont();
}
public LightEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setFont();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public LightEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
setFont();
}
/**
* This method is used to set the given font to the TextView.
*/
private void setFont() {
Typeface typeface = TypefaceCache.get(getContext().getAssets(), "fonts/Roboto-Light.ttf");
setTypeface(typeface);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}