开发者

Android onTouchListener, easier way to implement

Well, I have 16 buttons. I want to find an easier way to add them to an onTouch listener, I'm new to Android/java so I'm not sure what would work. Here's my code for that right now.

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main)

.

    Button btn_main1 = (Button) findViewById(R.id.cmd_main1);
    Button btn_main2 = (Button) findViewById(R.id.cmd_main2);
    Button btn_main3 = (Button) findViewById(R.id.cmd_main3);
    Button btn_main4 = (Button) findViewById(R.id.cmd_main4);
    Button btn_main5 = (Button) findViewById(R.id.cmd_main5);
    Butt开发者_运维技巧on btn_main6 = (Button) findViewById(R.id.cmd_main6);
    Button btn_main7 = (Button) findViewById(R.id.cmd_main7);
    Button btn_main8 = (Button) findViewById(R.id.cmd_main8);
    Button btn_main9 = (Button) findViewById(R.id.cmd_main9);
    Button btn_main10 = (Button) findViewById(R.id.cmd_main10);
    Button btn_main11 = (Button) findViewById(R.id.cmd_main11);
    Button btn_main12 = (Button) findViewById(R.id.cmd_main12);
    Button btn_main13 = (Button) findViewById(R.id.cmd_main13);
    Button btn_main14 = (Button) findViewById(R.id.cmd_main14);
    Button btn_main15 = (Button) findViewById(R.id.cmd_main15);
    Button btn_main16 = (Button) findViewById(R.id.cmd_main16);


    btn_main1.setOnTouchListener(tListener1);
    btn_main2.setOnTouchListener(tListener1);
    btn_main3.setOnTouchListener(tListener1);
    btn_main4.setOnTouchListener(tListener1);
    btn_main5.setOnTouchListener(tListener1);
    btn_main6.setOnTouchListener(tListener1);
    btn_main7.setOnTouchListener(tListener1);
    btn_main8.setOnTouchListener(tListener1);
    btn_main9.setOnTouchListener(tListener1);
    btn_main10.setOnTouchListener(tListener1);
    btn_main11.setOnTouchListener(tListener1);
    btn_main12.setOnTouchListener(tListener1);
    btn_main13.setOnTouchListener(tListener1);
    btn_main14.setOnTouchListener(tListener1);
    btn_main15.setOnTouchListener(tListener1);
    btn_main16.setOnTouchListener(tListener1);
}

Obviously, that's a terrible way to do it. I'm sure there's a way to do some sort of loop, or put the buttons in an array or something. Any help shortening this up is greatly appreciated!


Another way could be to add the buttons using code instead of XML.

However, just to clean up that code...

int[] buttonIds = new int[]{R.id.cmd_main1, R.id.cmd_main2}; // and so on

for(int buttonId : buttonIds) {
    Button b = (Button)findViewById(buttonId);
    b.setOnTouchListener(tListener1);
}

Also, an onClickListener is probably cleaner, depending on what you want the buttons to do. You could then also specify the onClickListener in XML:

<Button android:onClick="myClickHandler" />

and in the activity, add the corresponding method:

class MyActivity extends Activity {
public void myClickHandler(View target) {
    // Do stuff
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜