开发者

How do I activate the click event for a button on Android?

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
   }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜