Java代码优化 - 将两个功能合二为一

时间:2011-08-10 01:51:00

标签: java android

您好,我有这段代码:

this.firstBtn.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

        final CharSequence[] items = {"1", "2", "3"};
        AlertDialog.Builder builder = new AlertDialog.Builder(SlovnikoidActivity.this);
        builder.setTitle("test");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                langFrom.setText(items[item]);
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }
});

this.secondBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            final CharSequence[] items = {"1", "2", "3"};
            AlertDialog.Builder builder = new AlertDialog.Builder(SlovnikoidActivity.this);
            builder.setTitle("test");
            builder.setItems(items, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                    langFrom.setText(items[item]);
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
    });

只有两个不同的部分:this.firstBtnthis.secondBtn 有没有办法合并呢?例如,当我点击firstBtn时,它会调用函数foo(firstBtn)secondBtn调用foo(secondBtn),其余的会调用相同的函数吗? 我不确定语法等因为我现在是Java和Android开发的新手。

由于

2 个答案:

答案 0 :(得分:3)

您想要的相对容易。您可以定义一个实现OnClickListener的新类,并在两个位置使用此类的新实例。

class MyOnClickListener implements OnClickListener {
    public void onClick(View v) {
        final CharSequence[] items = {"1", "2", "3"};
        AlertDialog.Builder builder = new AlertDialog.Builder(SlovnikoidActivity.this);
        builder.setTitle("test");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                langFrom.setText(items[item]);
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }
}

您的代码可能如下所示:

this.firstBtn.setOnClickListener(new MyOnClickListener());
this.secondBtn.setOnClickListener(new MyOnClickListener());

显然,如果您的两个MyOnClickListener需要稍微不同,可以采取进一步的解决方案。这可以通过创建一个构造函数来完成,该构造函数接收您想要更改的参数并将它们存储为成员变量,然后在调用onClick(...)时使用:

class MyOnClickListener implements OnClickListener {
    private CharSequence[] items;

    public MyOnClickListener(CharSequence[] _items) {
        items = _items;
    }

    public void onClick(View v) {
        AlertDialog.Builder builder = new AlertDialog.Builder(SlovnikoidActivity.this);
        builder.setTitle("test");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                langFrom.setText(items[item]);
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }
}

然后可以这样使用:

final CharSequence[] items1 = {"1", "2", "3"};
final CharSequence[] items2 = {"2", "3", "4"};
this.firstBtn.setOnClickListener(new MyOnClickListener(items1));
this.secondBtn.setOnClickListener(new MyOnClickListener(items2));

更新

public class SlovnikoidActivity extends Activity {
    //slovnikoidActivity definition

    //inner class definition for MyOnClickListener
    class MyOnClickListener implements OnClickListener {
        private CharSequence[] items;

        public MyOnClickListener(CharSequence[] _items) {
            items = _items;
        }

        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(SlovnikoidActivity.this);
            builder.setTitle("test");
            builder.setItems(items, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                    langFrom.setText(items[item]);
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
    }
}

答案 1 :(得分:1)

作为nicholas.hauschild答案的替代方案,如果您不想单独上课,只需让您的活动实施View.OnClickListener

然后,将您的onClick(View v)方法移动到您的按钮代码所在的同一个类中。如果您需要区分不同的按钮,那么只需执行switch (v.getId())