定义在Android的按钮事件的最佳实践 [英] Best practice for defining button events in android
问题描述
我有一个布局
的XML其中包括几个按钮
取值定义。
I have a Layout
defined in XML which consists of several Button
s.
目前我做这在的OnCreate
的方法来对按钮定义事件处理程序:
Currently I am doing this in the OnCreate
method to define the event handlers against the buttons:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button newPicButton = (Button)findViewById(R.id.new_button);
newPicButton.setOnClickListener(btnListener);
..... similarly for other buttons too
.....
}
在按钮
的的onClick
事件,我启动照相机意图
来获得图像和 onActivityResult
回调里面,我再次将事件处理程序与设置查看
是这样的:
Inside of the Button
's onClick
event, I launch a camera Intent
to get a picture and inside the onActivityResult
callback I am again setting the event handlers along with setting the View
like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
setContentView(R.layout.main);
Button newPicButton = (Button)findViewById(R.id.new_button);
newPicButton.setOnClickListener(btnListener);
...similarly for other buttons too
}
我是新来的Android和重新定义每一次事件的这种做法似乎很肮脏的我。我想知道什么是定义按钮的事件处理程序的情况是这样的条款的最佳做法。
I am new to android and this approach of redefining an event every time seems quite dirty to me. I would like to know what is the best practice in terms of defining button event handlers in scenarios like this.
编辑:粘贴我的完整的类
pasting my complete class
public class CameraAppActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button newPicButton = (Button)findViewById(R.id.new_button);
newPicButton.setOnClickListener(btnListener);
}
//---create an anonymous class to act as a button click listener---
private OnClickListener btnListener = new OnClickListener()
{
public void onClick(View v)
{
//Intent newPicIntent = new Intent(v.getContext(), NewPictureActivity.class);
//startActivityForResult(newPicIntent, 0);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 999);
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
setContentView(R.layout.main);
Button newPicButton = (Button)findViewById(R.id.new_button);
newPicButton.setOnClickListener(btnListener);
//if I comment last two lines nothing happens when I click on button
}
的主要问题是
setContentView(R.layout.main);
Button newPicButton = (Button)findViewById(R.id.new_button);
newPicButton.setOnClickListener(btnListener);
重新注册在 onActivityResult
事件......是不是正确的做法?还是我做错了什么?因为如果我不重新注册事件没有任何反应,当我点击该按钮。
Re-registering events inside onActivityResult
.. is it right approach? Or am I doing something wrong? Because If I don't re-register event nothing happens when I click the button.
推荐答案
为什么不注册onClick事件中的XML布局,然后处理它在code。这是我会怎么做:
Why not registering onClick event in the XML layout and then handle it in the code. This is how I would do it:
<Button
android:id="@+id/my_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:onClick="onBtnClicked">
</Button>
和现在创造将处理点击的方法
and now create a method that would handle clicks
public void onBtnClicked(View v){
if(v.getId() == R.id.my_btn){
//handle the click here
}
}
另外,也可以单独设置OnClickListener在code每个项目。然后使用if / else或switch语句来确定原点。
Alternatively, you can set the OnClickListener individually for each item in the code. Then use the if/else or switch statements to determine the origin.
这样你就可以有一个处理所有按钮从一种布局方法之一。
This way you can have one method that handles all buttons from one layout.
更新:
虽然这是一个有效的方法,我会强烈建议的第二个选项。这是更清洁和更易于维护尤其是当您使用的碎片。
这篇关于定义在Android的按钮事件的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!