从另一个类android设置Text edittext活动

时间:2018-05-16 13:12:25

标签: android

我是一个想要从另一个班级设置文本的新手android程序员

以下是代码:

MainActivity.java

EventHandler eventHandler = new EventHandler();
reader.Events.addEventsListener(eventHandler);

这是另一个类但仍在MainActivity中

class EventHandler implements RfidEventsListener {
    // Read Event Notification
    public void eventReadNotify(RfidReadEvents e){
        TagData[] myTags = myReader.Actions.getReadTags(100);
        if (myTags != null)
        {
            for (int index = 0; index < myTags.length; index++)
            {
                System.out.println("Tag ID " + myTags[index].getTagID());
                //I want to settext here
            }
        }
    }
    // Status Event Notification
    public void eventStatusNotify(RfidStatusEvents e) {
        System.out.println("Status Notification: " + 
        e.StatusEventData.getStatusEventType());
    }
}

谢谢..问候

2 个答案:

答案 0 :(得分:0)

这可以通过以下方式轻松实现:(测试示例)

amaz

答案 1 :(得分:0)

当您在UI线程上执行代码时,您只能操作UI元素。

class EventHandler implements RfidEventsListener {
    // Read Event Notification
    public void eventReadNotify(RfidReadEvents e){
        TagData[] myTags = myReader.Actions.getReadTags(100);
        if (myTags != null)
        {
            for (int index = 0; index < myTags.length; index++)
            {
                System.out.println("Tag ID " + myTags[index].getTagID());

                //I want to settext here
                final String myText = myTags[index].getTagID();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        myEditText.setText(myText);
                    }
                });

            }
        }
    }
    // Status Event Notification
    public void eventStatusNotify(RfidStatusEvents e) {
        System.out.println("Status Notification: " + 
        e.StatusEventData.getStatusEventType());
    }
}