如何在另一个内部运行自定义功能?

时间:2015-02-27 17:50:34

标签: java android

我在android studio上构建复杂的应用程序,我有这个代码,我必须在点击它时把它放在另一个函数中:

                EditText e1 = (EditText) findViewById(R.id.Name);
                EditText e2 = (EditText) findViewById(R.id.Room);
                EditText e3 = (EditText) findViewById(R.id.Date);
                EditText e4 = (EditText) findViewById(R.id.Age);
                EditText e5 = (EditText) findViewById(R.id.Height);
                EditText e6 = (EditText) findViewById(R.id.Weight);
                EditText e7 = (EditText) findViewById(R.id.SerumC);
                EditText e8 = (EditText) findViewById(R.id.SUN);
                EditText e9 = (EditText) findViewById(R.id.ALB);

                SResult.setVisibility(View.INVISIBLE);
                r11b.setVisibility(View.INVISIBLE);
                iup.setVisibility(View.INVISIBLE);
                alert.setVisibility(View.INVISIBLE);

                rup.setVisibility(View.INVISIBLE);

                r11.setVisibility(View.INVISIBLE);
                e1.setVisibility(VISIBLE);
                e2.setVisibility(VISIBLE);
                e3.setVisibility(VISIBLE);
                e4.setVisibility(VISIBLE);
                e5.setVisibility(VISIBLE);
                e6.setVisibility(VISIBLE);
                e7.setVisibility(VISIBLE);
                e8.setVisibility(VISIBLE);
                e9.setVisibility(VISIBLE);
                Male.setVisibility(VISIBLE);
                Female.setVisibility(VISIBLE);
                Black.setVisibility(VISIBLE);
                NonBlack.setVisibility(VISIBLE);
                Stable.setVisibility(VISIBLE);
                NonStable.setVisibility(VISIBLE);
                Scrlabel.setVisibility(VISIBLE);
                clearH.setVisibility(VISIBLE);
                clearHN.setVisibility(VISIBLE);
                calcH.setVisibility(VISIBLE);
                calcHN.setVisibility(VISIBLE);
                menuH.setVisibility(VISIBLE);
                menuHN.setVisibility(VISIBLE);
                equatlab.setVisibility(VISIBLE);
                r12equ.setVisibility(VISIBLE);
                printC.setVisibility(View.INVISIBLE);
                printCH.setVisibility(View.INVISIBLE);
                homeC.setVisibility(View.INVISIBLE);
                homeCH.setVisibility(View.INVISIBLE);
                returnC.setVisibility(View.INVISIBLE);
                returnCH.setVisibility(View.INVISIBLE);
                par.setVisibility(VISIBLE);

但正如你所看到的那样长而且随着时间的推移我将面临一个难以在我的应用程序中进行任何更改的问题,因此我需要将其存储在自定义函数中并在单击按钮时执行此操作功能从这样开始:


public class visistore {
EditText e1 = (EditText) findViewById(R.id.Name);
                EditText e2 = (EditText) findViewById(R.id.Room);
                EditText e3 = (EditText) findViewById(R.id.Date);
                EditText e4 = (EditText) findViewById(R.id.Age);
                EditText e5 = (EditText) findViewById(R.id.Height);
                EditText e6 = (EditText) findViewById(R.id.Weight);
                EditText e7 = (EditText) findViewById(R.id.SerumC);
                EditText e8 = (EditText) findViewById(R.id.SUN);
                EditText e9 = (EditText) findViewById(R.id.ALB);

                SResult.setVisibility(View.INVISIBLE);
                r11b.setVisibility(View.INVISIBLE);
                iup.setVisibility(View.INVISIBLE);
                alert.setVisibility(View.INVISIBLE);

                rup.setVisibility(View.INVISIBLE);

                r11.setVisibility(View.INVISIBLE);
                e1.setVisibility(VISIBLE);
                e2.setVisibility(VISIBLE);
                e3.setVisibility(VISIBLE);
                e4.setVisibility(VISIBLE);
                e5.setVisibility(VISIBLE);
                e6.setVisibility(VISIBLE);
                e7.setVisibility(VISIBLE);
                e8.setVisibility(VISIBLE);
                e9.setVisibility(VISIBLE);
                Male.setVisibility(VISIBLE);
                Female.setVisibility(VISIBLE);
                Black.setVisibility(VISIBLE);
                NonBlack.setVisibility(VISIBLE);
                Stable.setVisibility(VISIBLE);
                NonStable.setVisibility(VISIBLE);
                Scrlabel.setVisibility(VISIBLE);
                clearH.setVisibility(VISIBLE);
                clearHN.setVisibility(VISIBLE);
                calcH.setVisibility(VISIBLE);
                calcHN.setVisibility(VISIBLE);
                menuH.setVisibility(VISIBLE);
                menuHN.setVisibility(VISIBLE);
                equatlab.setVisibility(VISIBLE);
                r12equ.setVisibility(VISIBLE);
                printC.setVisibility(View.INVISIBLE);
                printCH.setVisibility(View.INVISIBLE);
                homeC.setVisibility(View.INVISIBLE);
                homeCH.setVisibility(View.INVISIBLE);
                returnC.setVisibility(View.INVISIBLE);
                returnCH.setVisibility(View.INVISIBLE);
                par.setVisibility(VISIBLE);

}

然后,在我的主要活动中:

      button.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {

                return true;
            }
            if (event.getAction()== MotionEvent.ACTION_DOWN)
            {
  visistore
  }
    return true;
        }
    });

这是对的吗?!或者如果它错了所以我需要纠正我

2 个答案:

答案 0 :(得分:0)

如果我正确地理解了您的问题,那么只需定义boolean isButtonClicked

即可

制作此isButtonClicked = true onClick事件

然后将if(isButtonClicked)条件放入touchClick事件中。

希望你明白了

答案 1 :(得分:0)

您不想设置OnTouchListener而是OnClickListeneronClick()的{​​{1}}方法调用适当的函数。