Working with buttons in android

2019-08-20 21:47发布

Alright, so i've been making great progress on the app i'm trying to create, but most of the tutorials that i've been learning from only showcase the wondrous feature of having only one active widget inside the application at a time...

The thing is, my application requires 2 or more buttons and that's the part i'm partially stuck at. My code implements a "SetWordsBtn" shown below (everything else is declared),

public void onCreate(Bundle icicle) {

   super.onCreate(icicle);
   setContentView(R.layout.main);

   SetWordsBtn=(Button)findViewById(R.id.SetWordsBtn);

   SetWordsBtn.setOnClickListener(this);

}

which implements a onClick() like this:

public void onClick(View view) {

  startWords();

}

but what if i have another button that deletes the words such as "DelWordsBtn"? I was thinking i could declare both buttons simultaneously like this:

  SetWordsBtn=(Button)findViewById(R.id.SetWordsBtn);
  DelWordsBtn=(Button)findViewById(R.id.DelWordsBtn);

  SetWordsBtn.setOnClickListener(this);
  DelWordsBtn.setOnClickListener(this);

but what about the onClick() method? Does it automatically apply itself to both the buttons when i do this?

How am i able to declare a seperate onClick from each other so it both does different stuff when i click on either one of them?

I was thinking the answer could be something like this, but i dunno :

  //Declarations
  SetWordsBtn=(Button)findViewById(R.id.SetWordsBtn);
  DelWordsBtn=(Button)findViewById(R.id.DelWordsBtn);

  SetWordsBtn.setOnClickListener(setWordsView);
  DelWordsBtn.setOnClickListener(delWordsView);

  //onClick Functions

public void onClick(View setWordsView) {

  startWords();

}

public void onClick(View delWordsView) {

  deleteWords();

}

So it would actually link the startWords() function to the SetWordsBtn, and deleteWords() to DelWordsBtn...

Any clear cut explanation/form of help would be appreciated. Thanks in advance guys. :)

4条回答
Anthone
2楼-- · 2019-08-20 22:14

Drag and drop button on graghiclayout.xml ...>right click the button -->choose other properties....>choose inherited from view ---->click on click ....name it callme.

That will be shows like this:

xml file

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="76dp"
    android:layout_y="58dp"
    android:onClick="callme"
    android:text="Button" />

Run once your project:

Open src --->activity .java ----->, do the coding like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.main);

   but=(Button)findViewById(R.id.button1);
}

public void callme(View v)
{
//Do somthing
}
查看更多
放荡不羁爱自由
3楼-- · 2019-08-20 22:24

The typical convention is to just switch off of the ID of the View that is clicked. For example:

View.OnClickListener listener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.SetWordsBtn:
                startWords();
                break;
            case R.id.DelWordsBtn:
                deleteWords();
                break;
        }
    }
};

int[] ids = { R.id.SetWordsBtn, R.id.DelWordsBtn };

for(int i : ids) ((Button)findViewById(i)).setOnClickListener(listener);
查看更多
太酷不给撩
4楼-- · 2019-08-20 22:31

You can alternatively set up anonymous inner class(es) that listen, instead of having your Activity itself be the listener that implements OnClickListener. Example from the Android Button javadoc:

     button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             // Perform action on click
         }
     });

http://developer.android.com/reference/android/widget/Button.html

P.S. start your local variable names, and method names, with lower case letters -- upper case is for class names.

查看更多
在下西门庆
5楼-- · 2019-08-20 22:41

Where you suggested:

public void onClick(View setWordsView) {

  startWords();
}

public void onClick(View delWordsView) {

  deleteWords();
}

If you think about it, there is no difference in the two method declarations and you would get a build error (method signatures are the same, even though the method parameter, View, has a different name).

If I understand your question correctly then the answer given by kcoppock is correct. You also could define an Anonymous Class

查看更多
登录 后发表回答