0

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

我不想为 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);
                }
            });
        }});
4

1 回答 1

2

试试这个

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>
于 2012-09-28T10:48:56.147 回答