标签:
http://blog.csdn.net/wangjinyu501/article/details/22052187
//这个是View的一个回调接口 /** * Interface definition for a callback to be invoked when a view is clicked. */ public interface OnClickListener { /** * Called when a view has been clicked. * * @param v The view that was clicked. */ void onClick(View v); }
下面看一个简单的例子:
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{ private Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.button1); button.setOnClickListener(this); } @Override public void onClick(View v) { Toast.makeText(getApplication(), "OnClick", Toast.LENGTH_LONG).show(); } } 这就是一个很典型的例子,当然也可以这样写: import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class SSSS extends Activity { private Button button; private OnClickListener clickListener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } }; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); button = (Button)findViewById(R.id.button1); button.setOnClickListener(clickListener); } }
下面是View类的setOnClickListener方法,把和回调相关代码贴出来。什么贴它呢,因为Button继承于TextView,而TextView继承于View,在View里面处理的回调:
/** * */ public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource { /** * Listener used to dispatch click events. * This field should be made private, so it is hidden from the SDK. * {@hide} */ protected OnClickListener mOnClickListener; /** * * Register a callback to be invoked when this view is clicked. If this view is not * clickable, it becomes clickable. * * @param l The callback that will run * * @see #setClickable(boolean) */ public void setOnClickListener(OnClickListener l) { if (!isClickable()) { setClickable(true); } mOnClickListener = l; } /** * Call this view‘s OnClickListener, if it is defined. * * @return True there was an assigned OnClickListener that was called, false * otherwise is returned. */ public boolean performClick() { sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED); if (mOnClickListener != null) { playSoundEffect(SoundEffectConstants.CLICK); mOnClickListener.onClick(this); return true; } return false; } }
final Dialog dialog = new Dialog(this, R.style.MyDialogStyle); dialog.setContentView(R.layout.dialog_exit_train); dialog.show(); ImageButton ib_affirm = (ImageButton) dialog .findViewById(R.id.dialog_exit_ib_affirm); ImageButton ib_cancel = (ImageButton) dialog .findViewById(R.id.dialog_exit_ib_cancel); ib_affirm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { saveUserData(); dialog.dismiss(); TestActivity.this.finish(); } }); ib_cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } });
import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.text.TextPaint; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.TextView; import com.fanfou.app.opensource.R; /** * * */ public class AlertInfoDialog extends Dialog implements View.OnClickListener { //创建接口 public static interface OnOKClickListener { public void onOKClick(); } private final Context mContext; private TextView mTitleView; private TextView mTextView; private Button mButtonOk; private CharSequence mTitle; private CharSequence mText; //生命接口对象 private OnOKClickListener mClickListener; public AlertInfoDialog(final Context context, final String title, final String text) { super(context, R.style.Dialog); this.mContext = context; this.mTitle = title; this.mText = text; } private void init() { setContentView(R.layout.dialog_alert); this.mTitleView = (TextView) findViewById(R.id.title); final TextPaint tp = this.mTitleView.getPaint(); tp.setFakeBoldText(true); this.mTitleView.setText(this.mTitle); this.mTextView = (TextView) findViewById(R.id.text); this.mTextView.setText(this.mText); this.mButtonOk = (Button) findViewById(R.id.button_ok); this.mButtonOk.setOnClickListener(this); } @Override public void onClick(final View v) { final int id = v.getId(); switch (id) { case R.id.button_ok: cancel();//调用 if (this.mClickListener != null) { this.mClickListener.onOKClick(); } break; default: break; } } @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setBlurEffect(); init(); } protected void setBlurEffect() { final Window window = getWindow(); final WindowManager.LayoutParams lp = window.getAttributes(); // lp.alpha=0.8f; lp.dimAmount = 0.6f; window.setAttributes(lp); window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); // window.addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); } public void setMessage(final CharSequence message) { this.mText = message; this.mTextView.setText(this.mText); } public void setMessage(final int resId) { this.mText = this.mContext.getResources().getText(resId); this.mTextView.setText(this.mText); } //设置监听器 也就是实例化接口 public void setOnClickListener(final OnOKClickListener clickListener) { this.mClickListener = clickListener; } @Override public void setTitle(final CharSequence title) { this.mTitle = title; this.mTitleView.setText(this.mTitle); } @Override public void setTitle(final int resId) { this.mTitle = this.mContext.getResources().getText(resId); this.mTitleView.setText(this.mTitle); } }
方式和上面介绍的一样,感兴趣的朋友可以自己去实现其他效果的。
标签:
原文地址:http://www.cnblogs.com/zhujiabin/p/4644509.html