Android Button实现方式:要贴上源码 好烦~~~T_T
1.最常见的 this方法
<span style="font-family:Microsoft YaHei;font-size:18px;">package com.sky.button;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements 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 v) {
switch (v.getId()) {
case R.id.btn:
Toast.makeText(this, "this最常用", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}</span><span style="font-size: 18px;">
</span>
<span style="font-family:Microsoft YaHei;"><span style="font-size:18px;">package com.sky.button;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
// 方法二:匿名内部类
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
Toast.makeText(MainActivity.this, "匿名内部类", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
});
}
}</span>
</span>
3.自定义外部接口
package com.sky.button;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
// 方法三:自定义外部接口
btn.setOnClickListener(new MyClickListenner());
}
class MyClickListenner implements OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
Toast.makeText(MainActivity.this, "自定义外部接口", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
}
4.反射方式 -- 此方法需要在layout文件中定义:android:onClick="clickButton"
package com.sky.button;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
}
// 方法四:利用反射方式
public void clickButton(View v) {
switch (v.getId()) {
case R.id.btn:
Toast.makeText(this, "Hello World! -- 利用反射方式", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}