[关闭]
@codingpan 2015-05-13T16:19:58.000000Z 字数 613 阅读 208

Two ways to listen/response input event in Android

android mobile


Click Event

  1. Do this in XML.
    Notice: Do not do this for event in fragment, Why?
  1. //In your view xml file, set the onClick property
  2. android:id="@+id/button1"
  3. android:onClick="someMethod"
  4. //In your Activity, write the implementation of someMethod
  5. public void someMethod(View v) {
  6. int id = v.getId();
  7. if (id == R.id.button1) {
  8. //Do sth to response click event
  9. }
  10. }
  1. Implement onClickListener()
  1. Button btn = (Button) findViewById(R.id.mybutton);
  2. btn.setOnClickListener(new View.OnClickListener() {
  3. @Override
  4. public void onClick(View v) {
  5. someMethod(v);
  6. //or Do sth
  7. }
  8. });
  9. // some more code
  10. public void someMethod(View v) {
  11. // does something very interesting
  12. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注