新手:Android 按钮

发布于 2024-12-06 20:22:02 字数 680 浏览 1 评论 0原文

我有两个按钮 b1 和 b2 。如果按下按钮 b1 则执行查询 q1,否则如果按下按钮 b2 则执行另一个查询 q2。

if(b1_click)
{
  mCursor=//query
}
else if(b2_click)
{
  mCursor=//query
}

请告诉我如何实现这个。如何实现 b1_click 方法或任何告诉按钮被按下的内置方法。我尝试过,

Cursor c;
c=//querys

    if(b1.isPressed())
    {
        next.setOnClickListener
        (
            new View.OnClickListener()
            {
                @Override public void onClick(View v) {
                    c=db.getData1(); (getData1 method return cursor)
                }
            }
        );
    } 
   tv.append(c.getString(column_number) (tv=TextView)

   "Same as above for b2"

它是说光标(c)应该是最终的 帮助?

I have two button b1 and b2 .If button b1 is pressed then perform a query q1 else if button b2 is pressed then perform an another query q2.

if(b1_click)
{
  mCursor=//query
}
else if(b2_click)
{
  mCursor=//query
}

Please tell me how can i implement this.How to implement b1_click method or any inbuilt method which tell that button is pressed.I tried

Cursor c;
c=//querys

    if(b1.isPressed())
    {
        next.setOnClickListener
        (
            new View.OnClickListener()
            {
                @Override public void onClick(View v) {
                    c=db.getData1(); (getData1 method return cursor)
                }
            }
        );
    } 
   tv.append(c.getString(column_number) (tv=TextView)

   "Same as above for b2"

It is saying that cursor (c) should be final
Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

囍笑 2024-12-13 20:22:02

首先,我建议您阅读一些教程Hello Android,您可以另请遵循常见任务以及如何在 Android 中执行这些任务中的代码和示例。

如果你仔细阅读这篇文章,你会知道这很简单,就像这样:

public class MyActiviy extends Activity implements OnClickListener{

     protected void onCreate(Bundle savedInstance){
          super.onCreate(savedInstance);
          setContentView(R.layout.myLayout);
          findViewById(R.id.Button1).setOnClickListener(this);
          findViewById(R.id.Button2).setOnClickListener(this);
          //more code...
     }

     public void onClick(View v){
          switch(v.getId()){
              case R.id.Button1:
                  //Button1 pressedd...do stuff
                  break;
              case R.id.Button2:
                  //Button2 pressed...do some other stuff
                  break;
              default:
                  break;
          }
     }
}

First of all I would recommend going through some tutorials Hello Android and you could also follow the code and samples in Common Tasks and How to do them in Android.

If you would read this carefully you would know that is very simple, something like this:

public class MyActiviy extends Activity implements OnClickListener{

     protected void onCreate(Bundle savedInstance){
          super.onCreate(savedInstance);
          setContentView(R.layout.myLayout);
          findViewById(R.id.Button1).setOnClickListener(this);
          findViewById(R.id.Button2).setOnClickListener(this);
          //more code...
     }

     public void onClick(View v){
          switch(v.getId()){
              case R.id.Button1:
                  //Button1 pressedd...do stuff
                  break;
              case R.id.Button2:
                  //Button2 pressed...do some other stuff
                  break;
              default:
                  break;
          }
     }
}
池木 2024-12-13 20:22:02

尝试将 OnClickListeners 创建为私有类:

private class Button1ClickedListener implements OnClickListener {
  public void onClick( View v ) {
    //Do what needs to be done when button 1 is clicked.
  }
}

private class Button2ClickedListener implements OnClickListener {
  public void onClick( View v ) {
    //Do what needs to be done when button 2 is clicked.
  }
}

然后设置 onClick-listeners:

b1.setOnClickListener( new Button1ClickedListener() );
b2.setOnClickListener( new Button1ClickedListener() );

如果您需要 OnClickListeners 能够使用光标,则只需在 OnClickListeners 的父类中将其声明为私有字段即可。

Try creating your OnClickListeners as private classes:

private class Button1ClickedListener implements OnClickListener {
  public void onClick( View v ) {
    //Do what needs to be done when button 1 is clicked.
  }
}

private class Button2ClickedListener implements OnClickListener {
  public void onClick( View v ) {
    //Do what needs to be done when button 2 is clicked.
  }
}

Then you set the onClick-listeners:

b1.setOnClickListener( new Button1ClickedListener() );
b2.setOnClickListener( new Button1ClickedListener() );

If you need the OnClickListeners to be able to use the cursor, you just need to declare it as a private field in the parent class of the OnClickListeners.

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