如果我将一个 EditText 分配给另一个 EditText,为什么它们指向相同的值?
If I assign an EditText to another EditText, why do they point to the same value?
今天我遇到了这个非常有趣的情况。
假设:
从 XML
实例化了一个 EditText
。
EditText editText;
editText = findViewByID(R.id.editText_xml_id);
现在,我想听听 EditText
中焦点的变化。所以我写了下面的代码:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
Log.e("TAG", "Focus Changed in parent" );
}
});
到目前为止一切都很好,因为当焦点改变时,日志被触发。
现在是有趣的部分:
说,我想把同一个焦点听众,但两次!
所以我要做的是:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
Log.e("TAG", "Focus Changed in parent: First callback" );
}
});
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
Log.e("TAG", "Focus Changed in parent: Second callback" );
}
});
我所期望的是,因为 editText 实例已设置为监听 View.OnFocusChangeListeners
的两个实例,所以我认为结果会到达两个监听器。
但是后来我才意识到只有后面的监听器(最晚设置)在onFocuChange()内部发送了实际的回调;
这意味着,当焦点改变时,我期待:
Log.e("TAG", "Focus Changed in parent: First callback" );
Log.e("TAG", "Focus Changed in parent: Second callback" );
但我得到的是:
Log.e("TAG", "Focus Changed in parent: Second callback" );
仔细想想,我认为这是因为editText中设置的第一个监听器被后者替换了。因此只有一个 TAG 被击中。 (如有错误请指正)
在进一步试验时,我想如果我将 editText
分配给不同的 EditText
变量,然后为每个变量设置两个唯一的侦听器会怎么样?
所以我这样做了:
EditText childEditText1;
childEditText1 = editText;
然后我分配了两个不同的监听器:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
Log.e("TAG", "Focus Changed in child" );
}
});
childEditText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
Log.e("TAG", "Focus Changed in parent" );
}
});
在改变焦点时,我期望的是两个标签都被命中。这是因为现在同一个变量有两个不同的实例,它们每个都监听两个不同的侦听器。但同样,只有一个被击中。
期望值:
//Expected
Log.e("TAG", "Focus Changed in child" );
Log.e("TAG", "Focus Changed in parent" );
现实:
//Reality
Log.e("TAG", "Focus Changed in parent" );
无论如何,我的猜测是 EditText
实例都 指向 相同的 内存 space ?或者它是什么?因为否则两个标签都会被击中。
现在,如果有一些 String 并且其值正在更改,这将是预期的。
喜欢:
String str = "hi";
str1 = str;
str1 = str1 + " there";
Log.e("TAG", "str " + str );
Result: hi
Log.e("TAG", "str1 " + str1 );
Result : hi there
但是从上面的实验来看,就像:
Log.e("TAG", "str " + str );
Result: hi there
//which is not the case
这让我很烦恼。
请解释为什么 EditText 会出现这种异常,以及 Java 中是否有任何技术术语。并指出任何可以更好地解释这种情况的资源或书籍。我对 Android 并不陌生,但这绝对是。
谢谢!
his was because now there are two DIFFERENT instances of the same
variable, and they each listen to two DIFFERENT listeners. But again,
only one was hit. Expectation:
您正在对同一个引用调用 setter。最后一个获胜(它覆盖前一个)
Please explain why this sort of anomaly with EditText and if there is
an y technical term for this in Java.And do point to any resource or
books that explains such better. I'm not new to Android but this
definitely is.
分配引用不会复制对象。它与 EditText 无关
第一个问题:editText中设置的第一个监听器被后一个替换了:
原因:http://androidxref.com/9.0.0_r3/xref/frameworks/base/core/java/android/view/View.java#6318
//view.java
/**
* Register a callback to be invoked when focus of this view changed.
*
* @param l The callback that will run.
*/
public void setOnFocusChangeListener(OnFocusChangeListener l) {
getListenerInfo().mOnFocusChangeListener = l;
}
ListenerInfo getListenerInfo() {
if (mListenerInfo != null) {
return mListenerInfo;
}
mListenerInfo = new ListenerInfo();
return mListenerInfo;
}
http://androidxref.com/9.0.0_r3/xref/frameworks/base/core/java/android/view/View.java#4271
所以你可以看到,只能设置一个更改侦听器。第二个将取代第一个。
第二题:楼上的回答很好。
这是一个通用的编程概念。
1- editText.onFocusListener
为空。
2- editText.onFocusListener
由以下块更改。它现在打印第一个日志。
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
Log.e("TAG", "Focus Changed in child" );
}
});
3- editText.onFocusListener
由下面的块更改。它现在打印另一个日志。
childEditText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
Log.e("TAG", "Focus Changed in parent" );
}
});
这与 addOnChangeListener
完全不同。
以上是一个 set
函数,它不同于 add
函数。
希望一切顺利!
private EditText editTextone;
private Editext editTexttwo;
editTextone =(EditText) findViewByID(R.id.editText_xml_id_one);
editTexttwo =(EditText) findViewByID(R.id.editText_xml_id_one);
今天我遇到了这个非常有趣的情况。
假设:
从 XML
实例化了一个 EditText
。
EditText editText;
editText = findViewByID(R.id.editText_xml_id);
现在,我想听听 EditText
中焦点的变化。所以我写了下面的代码:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
Log.e("TAG", "Focus Changed in parent" );
}
});
到目前为止一切都很好,因为当焦点改变时,日志被触发。
现在是有趣的部分: 说,我想把同一个焦点听众,但两次! 所以我要做的是:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
Log.e("TAG", "Focus Changed in parent: First callback" );
}
});
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
Log.e("TAG", "Focus Changed in parent: Second callback" );
}
});
我所期望的是,因为 editText 实例已设置为监听 View.OnFocusChangeListeners
的两个实例,所以我认为结果会到达两个监听器。
但是后来我才意识到只有后面的监听器(最晚设置)在onFocuChange()内部发送了实际的回调;
这意味着,当焦点改变时,我期待:
Log.e("TAG", "Focus Changed in parent: First callback" );
Log.e("TAG", "Focus Changed in parent: Second callback" );
但我得到的是:
Log.e("TAG", "Focus Changed in parent: Second callback" );
仔细想想,我认为这是因为editText中设置的第一个监听器被后者替换了。因此只有一个 TAG 被击中。 (如有错误请指正)
在进一步试验时,我想如果我将 editText
分配给不同的 EditText
变量,然后为每个变量设置两个唯一的侦听器会怎么样?
所以我这样做了:
EditText childEditText1;
childEditText1 = editText;
然后我分配了两个不同的监听器:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
Log.e("TAG", "Focus Changed in child" );
}
});
childEditText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
Log.e("TAG", "Focus Changed in parent" );
}
});
在改变焦点时,我期望的是两个标签都被命中。这是因为现在同一个变量有两个不同的实例,它们每个都监听两个不同的侦听器。但同样,只有一个被击中。 期望值:
//Expected
Log.e("TAG", "Focus Changed in child" );
Log.e("TAG", "Focus Changed in parent" );
现实:
//Reality
Log.e("TAG", "Focus Changed in parent" );
无论如何,我的猜测是 EditText
实例都 指向 相同的 内存 space ?或者它是什么?因为否则两个标签都会被击中。
现在,如果有一些 String 并且其值正在更改,这将是预期的。 喜欢:
String str = "hi";
str1 = str;
str1 = str1 + " there";
Log.e("TAG", "str " + str );
Result: hi
Log.e("TAG", "str1 " + str1 );
Result : hi there
但是从上面的实验来看,就像:
Log.e("TAG", "str " + str );
Result: hi there
//which is not the case
这让我很烦恼。
请解释为什么 EditText 会出现这种异常,以及 Java 中是否有任何技术术语。并指出任何可以更好地解释这种情况的资源或书籍。我对 Android 并不陌生,但这绝对是。
谢谢!
his was because now there are two DIFFERENT instances of the same variable, and they each listen to two DIFFERENT listeners. But again, only one was hit. Expectation:
您正在对同一个引用调用 setter。最后一个获胜(它覆盖前一个)
Please explain why this sort of anomaly with EditText and if there is an y technical term for this in Java.And do point to any resource or books that explains such better. I'm not new to Android but this definitely is.
分配引用不会复制对象。它与 EditText 无关
第一个问题:editText中设置的第一个监听器被后一个替换了: 原因:http://androidxref.com/9.0.0_r3/xref/frameworks/base/core/java/android/view/View.java#6318
//view.java
/**
* Register a callback to be invoked when focus of this view changed.
*
* @param l The callback that will run.
*/
public void setOnFocusChangeListener(OnFocusChangeListener l) {
getListenerInfo().mOnFocusChangeListener = l;
}
ListenerInfo getListenerInfo() {
if (mListenerInfo != null) {
return mListenerInfo;
}
mListenerInfo = new ListenerInfo();
return mListenerInfo;
}
http://androidxref.com/9.0.0_r3/xref/frameworks/base/core/java/android/view/View.java#4271 所以你可以看到,只能设置一个更改侦听器。第二个将取代第一个。
第二题:楼上的回答很好。
这是一个通用的编程概念。
1- editText.onFocusListener
为空。
2- editText.onFocusListener
由以下块更改。它现在打印第一个日志。
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
Log.e("TAG", "Focus Changed in child" );
}
});
3- editText.onFocusListener
由下面的块更改。它现在打印另一个日志。
childEditText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
Log.e("TAG", "Focus Changed in parent" );
}
});
这与 addOnChangeListener
完全不同。
以上是一个 set
函数,它不同于 add
函数。
希望一切顺利!
private EditText editTextone;
private Editext editTexttwo;
editTextone =(EditText) findViewByID(R.id.editText_xml_id_one);
editTexttwo =(EditText) findViewByID(R.id.editText_xml_id_one);