创建和设置自定义属性的EditText [英] Creating and setting custom EditText attributes

查看:138
本文介绍了创建和设置自定义属性的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自定义属性添加到以编程方式创建的组EditTexts的。我必须通过一个Arduino设置了三个不同的传感器,并谨以此自定义属性由应用程序可以用来决定从读哪个传感器。

有没有一种方法来创建自定义属性,以便我可以声明,如设置 editTextX.setSensorType(0)

下面是code,我会使用这个有:

 如果(检查是否有一定的价值Ĵ){
  rowjlabel.setText(%);  从倾斜传感器//读取或dimensionj.setSensorType(倾斜);
}否则如果(检查是否有其他Ĵ值){
  rowjlabel.setText(,在);  从高度传感器//读取或dimensionj.setSensorType(高度);
}其他{
  rowjlabel.setText(,在);  从距离传感器//读取或dimensionj.setSensorType(距离);
}


解决方案

当然,你也可以继承的EditText并添加任何你需要的字段或方法。像这样的东西(注意,你将需要更多的code比这个,像一个构造函数):

 公共类MyEditText扩展的EditText {    ...    私人诠释mSensorType;    公共无效setSensorType(整型){
        mSensorType =类型;
    }    公众诠释getSensorType(){
        返回mSensorType;
    }
}

然后你只需要使用的地方的EditText的那类应用程序中。

I would like to add a custom property to a group of EditTexts that are created programmatically. I have three different sensors set up via an Arduino, and I would like this custom attribute to be used by the app to dictate which sensor to read from.

Is there a way to create a custom attribute so that I can declare the setting like editTextX.setSensorType(0)?

Here is the code that I'd be using this with:

if (check for certain j value) {
  rowjlabel.setText(" %");

  // read from tilt sensor, or "dimensionj.setSensorType(tilt);"
} else if (check for other j value) {
  rowjlabel.setText(" in.");

  // read from height sensor, or "dimensionj.setSensorType(height);"
} else {
  rowjlabel.setText(" in.");

  //read from distance sensor, or "dimensionj.setSensorType(distance);"
}

解决方案

Sure, you can subclass EditText and add any fields or methods you need. Something like this (note, you will need a bit more code than this, like a constructor):

public class MyEditText extends EditText {

    ...

    private int mSensorType;

    public void setSensorType(int type){
        mSensorType = type;
    }

    public int getSensorType(){
        return mSensorType;
    }
}

Then you just have to use that class in place of EditText in your application.

这篇关于创建和设置自定义属性的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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