How to make your click event and handler easy to read.
If you have one button event in your activity, you can add listener like this.
final String test = "test";
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), test, Toast.LENGTH_SHORT).show();
}
});
But this is rarely. Also you wrote it everywhere like this.
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), test, Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), test, Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.button3).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), test, Toast.LENGTH_SHORT).show();
}
});
// ...
Then your change one of them, change event ,You will be lost.
So I think the best way to manage you events is like this, for example :
public class ClickEventExampleActivity extends Activity implements OnClickListener {
private static final String TAG = "ClickEventExampleActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cropimage);
bindEvents();
}
public void bindEvents() {
findViewById(R.id.button).setOnClickListener(this);
findViewById(R.id.discard).setOnClickListener(this);
findViewById(R.id.image).setOnClickListener(this);
findViewById(R.id.rotateLeft).setOnClickListener(this);
findViewById(R.id.rotateRight).setOnClickListener(this);
findViewById(R.id.save).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
// TODO button clicked
break;
case R.id.discard:
// TODO discard clicked
break;
case R.id.image:
// TODO image clicked
break;
case R.id.rotateLeft:
// TODO rotateLet clicked
break;
case R.id.rotateRight:
// TODO rotateRight clicked
break;
case R.id.save:
// TODO save clicked
break;
default:
Log.v(TAG, "clicked other:" + v.getId());
break;
}
}
}
Then you find anyone of them ,you can go onClick and go one case.
Why ?
The advantage is not only easy to management, but also improves the efficiency.
So the same , compare in flow code.
public class HanlderEventsExampleActivity extends Activity {
private static final int HANDLER_SHOW_DIALOG = 1;
private static final int HANDLER_SHOW_MESSAGE = 2;
private static Handler mBadHandler = new Handler();
private static Handler mGoodHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case HANDLER_SHOW_DIALOG:
break;
case HANDLER_SHOW_MESSAGE:
break;
default:
break;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBadHandler = new Handler();
// bad style
mBadHandler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO something
}
}, 1000);
mBadHandler.post(new Runnable() {
@Override
public void run() {
// TODO something
}
});
// good style
{
// show dialog
Message msg = new Message();
msg.what = HANDLER_SHOW_DIALOG;
Bundle b = new Bundle();
b.putString("key", "test");
msg.setData(b);
mGoodHandler.sendMessage(msg);
// show message
mGoodHandler.sendEmptyMessage(HANDLER_SHOW_MESSAGE);
//
mGoodHandler.sendEmptyMessageDelayed(HANDLER_SHOW_MESSAGE, 10000);
}
}
}
Now you can , If this is an anonymous class, in every time when using the newly created a class. If I recommend is used this way, so you don't have to create, every time is used directly, don't need to create.
handler :
If you use an anonymous class, will go up a new thread for their execution. Again to update the UI thread if I recommended way is to use, then you need more than one thread to communicate with the UI thread. Specific how Handler implementation, I have no in-depth study, but more than one thread, or a little more overhead is yes.
So I think, the development of Android Java code using the above best speak this kind of style, good read good understanding, also promote efficiency, why not?
Above is only personal point of view, if don't agree, please give comments, or take the facts to prove my point of view is wrong!
评论
发表评论