多次调用一个方法-android代码

多次调用一个方法-android代码,android,Android,我的布局有4个按钮。一个是“正确”答案,另外三个是“错误”答案。 我希望在按下3个“错误”按钮之一时显示相同的弹出窗口。这是我得到的密码 我不想重复这三个按钮中的每一个代码,我如何调用相同的代码,不同的按钮名称 ImageButton rredButton=(ImageButton)findViewById(R.id.RredButton); rredButton.setOnClickListener(new View.OnClickListener() { @

我的布局有4个按钮。一个是“正确”答案,另外三个是“错误”答案。 我希望在按下3个“错误”按钮之一时显示相同的弹出窗口。这是我得到的密码

我不想重复这三个按钮中的每一个代码,我如何调用相同的代码,不同的按钮名称

    ImageButton rredButton=(ImageButton)findViewById(R.id.RredButton);
    rredButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            LayoutInflater layoutInflater
            = (LayoutInflater)getBaseContext()      
            .getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupView = layoutInflater.inflate(R.layout.popupright, null);
            final PopupWindow popupWindow = new PopupWindow(               
                    popupView,                
                    LayoutParams.WRAP_CONTENT,                       
                    LayoutParams.WRAP_CONTENT);                            

            Button btnDismiss = (Button)popupView.findViewById(R.id.nextscreen);             
            btnDismiss.setOnClickListener(new Button.OnClickListener(){     
                @Override     
                public void onClick(View v) {      
                    Intent myintent1 = new Intent(colorActivity.this,LearningTimeMenu.class);
                    startActivity(myintent1);
                }
            });
        }});
试试这个

private void createPopUP()
{
    LayoutInflater layoutInflater
            = (LayoutInflater)getBaseContext()      
            .getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupView = layoutInflater.inflate(R.layout.popupright, null);
            final PopupWindow popupWindow = new PopupWindow(               
                    popupView,                
                    LayoutParams.WRAP_CONTENT,                       
                    LayoutParams.WRAP_CONTENT);                            

            Button btnDismiss = (Button)popupView.findViewById(R.id.nextscreen);             
            btnDismiss.setOnClickListener(new Button.OnClickListener(){     
                @Override     
                public void onClick(View v) {      
                    Intent myintent1 = new Intent(colorActivity.this,LearningTimeMenu.class);
                    startActivity(myintent1);
                }
            });

}

ImageButton rredButton=(ImageButton)findViewById(R.id.RredButton);
rredButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {

        createPopUP();

    }});
否则,在xml文件中,使用

<Button ..........
 android: onClick ="createPopUP"
</Button>

谢谢。这起作用了。使用“私有”无效与“受保护”无效有什么特别的原因吗?