How do I activate the click event for a button on Andr开发者_JS百科oid?
I'm not sure if you want to "click" the button via java or listen for the click event.
The listening is descirbed correctly by Graham
To automaticly click the Button you could call
Button b= (Button) findViewById(R.id.button1);
b.performClick();
In your layout file, include this in the button definition:
android:onClick="myClickHandler"
In your activity code, implement the handler:
void myClickHandler(View view) {
...
}
You push the button (please elaborate if that wasn't the answer you were looking for)
I would read through this document. It contains all you need and more:
http://d.android.com/guide/topics/ui/ui-events.html
I hope It will helpful for you.
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
}
@Override
public void OnClick(View view) {
//code
}
}
精彩评论