Android点击事件的几种实现方式
sc4003
8年前
<ol> <li> <p><strong>在每次使用时新实现一个OnClickListener接口</strong></p> <pre> <code class="language-java">public class MainActivity extends Activity { private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btnDoSomething); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { doSomething(v); } }); } public void doSomething(View view) { /*do something here*/ } }</code></pre> </li> <li> <p><strong>让这个页面类实现OnClickListener接口</strong></p> <pre> <code class="language-java">public class MainActivity extends Activity implements View.OnClickListener{ private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btnDoSomething); btn.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnDoSomething: doSomething(v); break; default: break; } } public void doSomething(View view) { /*do something here*/ } }</code></pre> </li> <li> <p><strong>多组合少继承的原则,把OnClickListener接口作为一个属性放到页面类中</strong></p> <pre> <code class="language-java">public class MainActivity extends Activity{ private Button btn; private View.OnClickListener clickListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); clickListener = new View.OnClickListener() { @Override public void onClick(View v) { doSomething(v); } }; btn = (Button) findViewById(R.id.btnDoSomething); btn.setOnClickListener(clickListener); } public void doSomething(View view) { /*do something here*/ } }</code></pre> </li> <li> <p><strong>把类中的点击事件看作是一个对象,新建一个类来实现这些事件</strong></p> <pre> <code class="language-java">public class ClickListener implements View.OnClickListener{ @Override public void onClick(View v) { doSomething(v); } private void doSomething(View view) { /*do something here*/ } }</code></pre> <p>在使用的时候,新建或者组合使用</p> <pre> <code class="language-java">public class MainActivity extends Activity{ private Button btn; private ClickListener clickListener = new ClickListener(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btnDoSomething); btn.setOnClickListener(clickListener); } }</code></pre> </li> <li> <p><strong>在java类中定义好点击事件要执行的方法,在布局文件xml中调用</strong></p> <p>activity_main.xml</p> <pre> <code class="language-java"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.vip.veblonwang.myapplication.MainActivity"> <Button android:id="@+id/btnDoSomething" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="share" android:onClick="doSomething"/><!-- 这句是关键,点击时执行MainActivity中的doSomething方法 --> </LinearLayout></code></pre> <p><strong>MainActivity .java</strong></p> <pre> <code class="language-java">public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void doSomething(View view) { //方法必须定义为public /*do something here*/ } }</code></pre> </li> </ol> <p>个人理解就上面几种实现方式了~</p> <p>推荐使用 <strong>第三、四种</strong> 方式,其他方式不推荐</p> <p> </p> <p>来自:http://www.jianshu.com/p/926f4f6c1d74</p> <p> </p>