安卓:从Java code人工呼叫监听器 [英] Android: Call listener manually from java code
问题描述
有没有办法从code手动调用一个监听?
Is there any way to call a listener manually from code?
更多的背景信息:我用一个微调和一个datepicker。随着微调你可以选择呆在家里的理由(也许你生病了,也许你有休假),并与的DatePicker你可以选择你不会是多久可用的日期。有了这两条信息我建立了一个TextView字符串,并显示在一个紧凑的方式相同的数据。字符串的构建过程是由一些听众能识别在两个控件中的一个变化和建设,并成立了新的字符串设定。
如果我启动该程序并读取从服务器的一些数据字符串不会建(显然,因为没有什么变化,也没有监听器将调用)。
More background information: I use a Spinner and a DatePicker. With the Spinner you can choose a reason for staying at home (maybe your ill, maybe you have vacation) and with the DatePicker you can choose the date how long you will not be available. With these two pieces of information I build up a string for a TextView and show the same data in a compact way. The building process for the string is set by some listeners which recognize changes on one of the two controls and build and set up the new string. If I start the program and read some data from a server the string will not be build (clearly because nothing changed and no listener will called).
解决方法是建立在自己的 onLoaddata()
方法的字符串。但我认为这将是平滑的方法来调用一个监听器打造的字符串我。我也可以呼的监听器,如果我只是做一些假 .updateDate
,但我不认为这是创造无用来电个好主意......
The workaround is to build the string in my own onLoaddata()
method. But I think it would be smoother way to call one listener to build the string for me. I also can "call" a listener if I just do some fake .updateDate
but I don't think it’s a good idea to create useless calls...
你也许有人有一个很好的提示我?
Maybe someone of you have a good hint for me?
推荐答案
使用下面的模式
public class YourActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
// Some initialization here
findViewById(R.id.some_button).setOnClickListener(this);
...
// Here you want to update your view
updateTextView();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.some_button:
// Here you also want to update your view
updateTextView();
break;
...
}
}
private void updateTextView() {
// Here you update your view
...
}
}
这篇关于安卓:从Java code人工呼叫监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!