2012-09-28 19 views
0

我的布局有4个按钮。一个是“正确的”答案,另外三个是“错误的”答案。 我想与被压3“错”的一个按钮来显示相同​​的弹出窗口。这是我得到的代码。调用一个方法多次 - Android的代码

我不想重复此代码为每个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); 
       } 
      }); 
     }}); 

回答

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> 
+0

谢谢。这工作。是否有任何特别的理由使用“私人”无效与“保护”无效? – Jasma