在扩展Android的一个的EditText。我究竟做错了什么? [英] Extending a EditText in Android. What am I doing wrong?

查看:119
本文介绍了在扩展Android的一个的EditText。我究竟做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图让在Android中使用自定义控制的把握。但是,我的应用程序崩溃在试图创建活动。这里的code:

 包com.myApp;
进口android.content.Context;
进口android.widget.EditText;
进口android.view.View;
进口android.view.View.OnClickListener;

公共类MyEditText扩展的EditText实现OnClickListener {

    公共MyEditText(上下文的背景下){
        超(上下文);
        // TODO自动生成构造函数存根
    }
    公共无效FlashBorder()
    {
        //做一些自定义操作
    }
    @覆盖
    公共无效的onClick(视图v){
        // TODO自动生成方法存根
        EditText上的txt =(EditText上)V;
        txt.selectAll();
    }
}
 

下面是布局的xml:

< com.myApp.MyEditText      机器人:ID =@ + ID / edtTaskName      机器人:layout_height =WRAP_CONTENT      机器人:layout_width =match_parent/>

解决方案

您需要实现这些构造函数:

 公共MyEditText(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);

}
公共MyEditText(上下文的背景下){
    超(上下文);

}

公共MyEditText(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
    超(背景下,ATTRS,defStyle);

}
 

例如尽量做到以下几点:

 公共MyEditText(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);

Log.i(索引0属性名称,attrs.getAttributeName(0));

    }
 

你会得到你的logcat如下:

 属性名称索引0 = ID
 

所以要提供这些XML attrbutes的超级类(EditText上),你需要重写这些建设者。

希望有所帮助。

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:

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:

<com.myApp.MyEditText
     android:id="@+id/edtTaskName"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"/> 

解决方案

You will need to implement these constructors:

public MyEditText(Context context, AttributeSet attrs) {
    super(context, attrs);

}
public MyEditText(Context context) {
    super(context);

}

public MyEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

}

for example try to do the following :

public MyEditText(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 :

attribute name at index 0 = id 

so to deliver these XML attrbutes to the Super class (EditText) you have to override these constructers .

Hope that Helps .

这篇关于在扩展Android的一个的EditText。我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆