Android onClick 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3733858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android onClick method
提问by bindal
I have two onclick method in android project
我在android项目中有两个onclick方法
clr=(Button)findViewById(R.id.Button01);
clr.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tv1.setText("CLR");
et1.setText("");
refrigerant = "";
pres = "";
temperature = "";
superheat_oda = 0;
sub_cool = 0;
}
});
And i have onther onClick method in which i have to call that method directly
而且我有 onClick 方法,我必须直接调用该方法
prs=(Button)findViewById(R.id.Button02);
prs.setOnClickListener(new OnClickListener() {
----- I have to call that method---
}
});
Is there Any Solution for this?
有什么解决方案吗?
回答by benvd
You want to call the first onClick
from the second? Just extract the contents of your first onClick in a separate method and call that method from each onClick
.
你想onClick
从第二个调用第一个?只需在单独的方法中提取第一个 onClick 的内容,然后从每个onClick
.
Edit: As per st0le's comment, you can do what you want by calling clr.performClick()
. (Didn't know that.) Still, extracting it into a separate method seems cleaner.
编辑:根据 st0le 的评论,您可以通过调用clr.performClick()
. (不知道。)不过,将它提取到一个单独的方法中似乎更干净。
回答by Ibraheem Saoud
you can do something like this in the XML file
你可以在 XML 文件中做这样的事情
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="some_function" />
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="some_function" />
and put this function in the Java file
并将这个函数放在 Java 文件中
public void some_function(View view) {
// stuff...
}
and put the some_function in both "onClick"s
并将 some_function 放在两个“onClick”中
回答by Nguyen Minh Binh
You should turn to use the simplest way that I always do as below:
你应该转向使用我总是做的最简单的方法,如下所示:
@Override
public void onCreate(Bundle savedInstanceState) {
button1.setOnClickListener(onClickListener);
button2.setOnClickListener(onClickListener);
}
private OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(final View v) {
switch(v.getId()){
case R.id.button1:
//DO something
break;
case R.id.button2:
//DO something
break;
}
}
};
回答by Simon
I would recommend to use the same OnClickListener for both buttons if both buttons really have to do the same thing:
如果两个按钮真的必须做同样的事情,我会建议对两个按钮使用相同的 OnClickListener:
OnClickListener l=new OnClickListener() {
public void onClick(View v) {
tv1.setText("CLR");
et1.setText("");
refrigerant = "";
pres = "";
temperature = "";
superheat_oda = 0;
sub_cool = 0;
}
};
clr=(Button)findViewById(R.id.Button01);
clr.setOnClickListener(l);
prs=(Button)findViewById(R.id.Button02);
prs.setOnClickListener(l);
or if its not exactly the same you could access the listener method by l.onClick(null); manually..
或者如果它不完全相同,您可以通过 l.onClick(null) 访问侦听器方法;手动..