标签:android
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1)点击按钮的实现类接口; //弊端,按钮多的话,代码冗余;</span>1.findViewByID();
}
在低下附上源代码MainActivity.java:
package com.weibo.onclick; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.Toast; import android.os.Build; public class MainActivity extends ActionBarActivity implements OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); Button btn_click1 =(Button) findViewById(R.id.btn_click1); Button btn_click2 =(Button) findViewById(R.id.btn_click2); Button btn_click3 =(Button) findViewById(R.id.btn_click3); Button btn_click4 =(Button) findViewById(R.id.btn_click4); /* //1.内部类方法获取按钮 btn_click1.setOnClickListener(new MyOnClikListener()); btn_click2.setOnClickListener(new MyOnClikListener2()); btn_click3.setOnClickListener(new MyOnClikListener3()); */ /* //2.匿名内部类实现 btn_click1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "按钮按钮1", 0).show(); } }); btn_click2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "按钮按钮2", 0).show(); } });*/ /* //3.用switch方法实现 btn_click1.setOnClickListener(this); btn_click2.setOnClickListener(this); btn_click3.setOnClickListener(this); btn_click4.setOnClickListener(this);*/ } class MyOnClikListener implements OnClickListener{ @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "按按钮钮1", 0).show(); } } class MyOnClikListener2 implements OnClickListener{ @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "按按钮钮2", 0).show(); } } class MyOnClikListener3 implements OnClickListener{ @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "按按钮钮3", 0).show(); } } @Override public void onClick(View v) { switch(v.getId()){ case R.id.btn_click1: Toast.makeText(this, "bt1", 0).show(); break; case R.id.btn_click2: Toast.makeText(this, "bt2", 0).show(); break; case R.id.btn_click3: Toast.makeText(this, "bt3", 0).show(); break; default : Toast.makeText(this, "bt4", 0).show(); } } public void 哈哈(View v){ Toast.makeText(this, "哈哈", 0).show(); } public void 点点(View v){ Toast.makeText(this, "点点", 0).show(); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:android
原文地址:http://blog.csdn.net/u011706736/article/details/46733791