我怎样才能在点击按钮android上调用以下void?
我有以下代码,我想将此代码称为 onclick 按钮 我该怎么做?
public static void main(String[] args) throws Exception {
String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
String search = "stackoverflow";
String charset = "UTF-8";
URL url = new URL(google + URLEncoder.encode(search, charset));
Reader reader = new InputStreamReader(url.openStream(), charset);
GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);
// Show title and URL of 1st result.
System.out.println(results.getResponseData().getResults().get(0).getTitle());
System.out.println(results.getResponseData().getResults().get(0).getUrl());
}
I have the following code and i want to call this code onclick button
how can i do it?
public static void main(String[] args) throws Exception {
String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
String search = "stackoverflow";
String charset = "UTF-8";
URL url = new URL(google + URLEncoder.encode(search, charset));
Reader reader = new InputStreamReader(url.openStream(), charset);
GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);
// Show title and URL of 1st result.
System.out.println(results.getResponseData().getResults().get(0).getTitle());
System.out.println(results.getResponseData().getResults().get(0).getUrl());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先你应该改变一些事情。您不应将该方法命名为
main
。在 android 中你不使用 main 方法。同样在 android 中,您不使用 System.out.println ,而是使用 Android Log 类。我会将方法更改为如下所示:
然后,当您定义
OnClickListener
时,只需在onClick()
方法中调用上述方法:First you should change a few things. You shouldn't name the method
main
. In android you don't use a main method. Also in android you don't useSystem.out.println
but instead you use the AndroidLog
class.I would change the method to look like this:
Then when you define your
OnClickListener
simply invoke the above method in theonClick()
method:假设被单击的按钮名为 myButton。然后你会这样做:
我拿出了 System.out.println 因为你不能在 android 中真正做到这些。您可以尝试打印到日志、制作 Toast 或进行其他类型的输出。
Let's say the button that's getting clicked is called myButton. You'd then do:
I took out the System.out.println because you can't really do those in android. You could try printing to the Log, making Toasts, or doing some other sort of output.