简体   繁体   English

Android中来自不同类的Java使用方法

[英]Java Using Method from Different Class for Android

I'm very new to Java and OOP. 我是Java和OOP的新手。 Here is what I've got: 这是我所拥有的:

public class AmountChanged implements View.OnFocusChangeListener {

    @Override
    public void notFocused(View edittext1, boolean focused) {

//Do this awesome stuff

}

How do I instantiate and use this on one of my editText boxes in the mainActivity? 如何在mainActivity的其中一个editText框上实例化并使用它? I have already declared the editText boxes in the onCreate method. 我已经在onCreate方法中声明了editText框。

Let's imagine you created an EditBox : 假设您创建了一个EditBox

EditText editText = new EditText(this);

To set focus change listener, you should provide OnFocusChangeListener instance to the setOnFocusChangeListener . 要设置焦点更改侦听器,应将OnFocusChangeListener实例提供给setOnFocusChangeListener Since AmountChanged implements OnFocusChangeListener , you can do the following: 由于AmountChanged实现了OnFocusChangeListener ,因此您可以执行以下操作:

editText.setOnFocusChangeListener(new AmountChanged());

If you are going to use the same listener on many EditText items, you can save this listener as a variable somewhere: 如果要在许多EditText项目上使用相同的侦听器,则可以将此侦听器另存为变量:

View.OnFocusChangeListener myListener = new AmountChanged();
...
editText.setOnFocusChangeListener(myListener);

In your class where you are writing the code for the editext,in that activity's onCreate() method, you need to write 在您要为editext编写代码的类中,该活动的onCreate()方法中,您需要编写

yourEditext.setOnFocusChangeListener(new AmountChanged());

Also give an eye to this please as you can use anonymous classes too. 也请注意这一点,因为您也可以使用匿名类。

In the onCreate method where you have the editText box(es) you want to use this with, 在要使用它的editText框的onCreate方法中,

View.OnFocusChangeListener ac = new AmountChanged();
editText.setOnFocusChangeListener(ac);

From the View Android Developer Guide , 在《 View Android开发人员指南》中

Set up listeners: Views allow clients to set listeners that will be notified when something interesting happens to the view. 设置侦听器:视图允许客户端设置侦听器,当视图中发生一些有趣的事情时,将通知这些侦听器。 For example, all views will let you set a listener to be notified when the view gains or loses focus. 例如,所有视图将允许您设置一个侦听器,以在该视图获得或失去焦点时得到通知。 You can register such a listener using setOnFocusChangeListener(View.OnFocusChangeListener) . 您可以使用setOnFocusChangeListener(View.OnFocusChangeListener)注册这样的侦听器。 Other view subclasses offer more specialized listeners. 其他视图子类提供更专业的侦听器。 For example, a Button exposes a listener to notify clients when the button is clicked. 例如,一个Button暴露一个监听器单击按钮时通知客户。

Listener in android means that it is gonna listen to some event( OnTouchListener , OnClickListener , OnFocusChangedListener etc.). android中的Listener意味着它将监听某些事件( OnTouchListenerOnClickListenerOnFocusChangedListener等)。 As you see OnFocusChangedListener interface is announced inside View class, in scope of Android it usually means that any child of View can produce this event, so you need to "listen" to those events. 如您所见,在View类内部宣布了OnFocusChangedListener接口,在Android范围内,这通常意味着View任何子级都可以产生此事件,因此您需要“监听”这些事件。

In scope of EdiText what you have to do is something like this: 在EdiText的范围内,您需要做的是这样的:

editText.setOnFocusChangedListener(new AmmountChanged());

EdiText is a child of View. EdiText是View的子级。 So we are start "listening" to all OnFocusChanged events that will happen inside editText by registering our instance implementation OnFocusChangeListener. 因此,通过注册实例实现OnFocusChangeListener,我们开始“监听” editText内部将发生的所有OnFocusChanged事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM