如何制作更短的代码

时间:2015-08-09 11:44:45

标签: android if-statement switch-statement onclicklistener popupmenu

这是我第一个应用程序的代码的一部分。目前我只添加了2个默认值为gab的TextViews,点击它们弹出PopupMenu有6个值。通过单击PopupMenu中的任何值,TextView的值将更改为单击的值(不更改其他未单击的TextView值)。我计划添加100或甚至更多这种TextViews。为每个人编写,复制和编辑ID等都会太乱。

如何缩短这个? 值的数量不会改变(6),ViewId的数量(例如augli_mervieniba和bites_mervieniba)将是巨大的。

    public void popup_mervieniba(final View view)
    {
        PopupMenu popupMenu = new PopupMenu(this, view);
        MenuInflater menuInflater = popupMenu.getMenuInflater();
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

如果单击第一个TextView,则此部分用于此

              if (view==findViewById(R.id.augli_mervieniba))

                    switch (item.getItemId()) {
                        case R.id.gab:
                            TextView t_gab = (TextView) findViewById(R.id.augli_mervieniba);
                            t_gab.setText(R.string.gab);
                            return true;
                        case R.id.iep:
                            TextView t_iep = (TextView) findViewById(R.id.augli_mervieniba);
                            t_iep.setText(R.string.iep);
                            return true;
                        case R.id.g:
                            TextView t_g = (TextView) findViewById(R.id.augli_mervieniba);
                            t_g.setText(R.string.g);
                            return true;
                        case R.id.kg:
                            TextView t_kg = (TextView) findViewById(R.id.augli_mervieniba);
                            t_kg.setText(R.string.kg);
                            return true;
                        case R.id.ml:
                            TextView t_ml = (TextView) findViewById(R.id.augli_mervieniba);
                            t_ml.setText(R.string.ml);
                            return true;
                        case R.id.l:
                            TextView t_l = (TextView) findViewById(R.id.augli_mervieniba);
                            t_l.setText(R.string.l);
                            return true;
                }

如果单击第二个TextView,则此部分用于此

            else if (view==findViewById(R.id.bietes_mervieniba))
                    switch (item.getItemId()) {
                        case R.id.gab:
                            TextView t_gab = (TextView) findViewById(R.id.bietes_mervieniba);
                            t_gab.setText(R.string.gab);
                            return true;
                        case R.id.iep:
                            TextView t_iep = (TextView) findViewById(R.id.bietes_mervieniba);
                            t_iep.setText(R.string.iep);
                            return true;
                        case R.id.g:
                            TextView t_g = (TextView) findViewById(R.id.bietes_mervieniba);
                            t_g.setText(R.string.g);
                            return true;
                        case R.id.kg:
                            TextView t_kg = (TextView) findViewById(R.id.bietes_mervieniba);
                            t_kg.setText(R.string.kg);
                            return true;
                        case R.id.ml:
                            TextView t_ml = (TextView) findViewById(R.id.bietes_mervieniba);
                            t_ml.setText(R.string.ml);
                            return true;
                        case R.id.l:
                            TextView t_l = (TextView) findViewById(R.id.bietes_mervieniba);
                            t_l.setText(R.string.l);
                            return true;
                }

代码结束

return false;
            }
        });
        menuInflater.inflate(R.menu.popup_mervieniba, popupMenu.getMenu());
        popupMenu.show();
    }

1 个答案:

答案 0 :(得分:-1)

这样做;

ActivityClass
{ private TextView tv1, tv2.......
onCreate()
{
tv1 = (TextView) findViewById(); 
tv2 = (TextView ) findViewById();
}

switch()
{
case R.id.gap: 
tv1.setText(); 
return;
………………
}