我怎样才能在点击按钮android上调用以下void?

发布于 2024-12-10 18:28:51 字数 675 浏览 1 评论 0原文

我有以下代码,我想将此代码称为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

格子衫的從容 2024-12-17 18:28:51

首先你应该改变一些事情。您不应将该方法命名为 main。在 android 中你不使用 main 方法。同样在 android 中,您不使用 System.out.println ,而是使用 Android Log 类。

我会将方法更改为如下所示:

public static void doSomething() 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.
    String tag = "myTag";
    Log.d(tag, results.getResponseData().getResults().get(0).getTitle());
    Log.d(tag, results.getResponseData().getResults().get(0).getUrl());
}

然后,当您定义 OnClickListener 时,只需在 onClick() 方法中调用上述方法:

public void onClick(View v){
    doSomething();
}

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 use System.out.println but instead you use the Android Log class.

I would change the method to look like this:

public static void doSomething() 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.
    String tag = "myTag";
    Log.d(tag, results.getResponseData().getResults().get(0).getTitle());
    Log.d(tag, results.getResponseData().getResults().get(0).getUrl());
}

Then when you define your OnClickListener simply invoke the above method in the onClick() method:

public void onClick(View v){
    doSomething();
}
绝對不後悔。 2024-12-17 18:28:51

假设被单击的按钮名为 myButton。然后你会这样做:

myButton.addOnClickListener(new View.OnClickListener(){
   public void onCick(View view){
     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);
   });

我拿出了 System.out.println 因为你不能在 android 中真正做到这些。您可以尝试打印到日志、制作 Toast 或进行其他类型的输出。

Let's say the button that's getting clicked is called myButton. You'd then do:

myButton.addOnClickListener(new View.OnClickListener(){
   public void onCick(View view){
     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);
   });

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文