0

/ **如何在其他方法onclick监听器中更改框架布局背景和文本,我们在同一个活动类的ocreate()方法中添加了该监听器?*/

 public class MainActivity extends Acitvity{ 

               public void onCreate(Bundle savedInstanceState){    
                     super.onCreate(savedInstanceState);    
                     setContentView(R.layout.mainView);     **//Main view**  
                     final FrameLayout rootLayout = (FrameLayout)findViewById    (R.id.mainViewRootLayout);    
                     FrameLayout ribbonLayout = (FrameLayout)findViewById(R.id.mainribbonLayout);    
                     TextView ribbonText = (TextView)findViewById(R.id.mainribbonLayoutText);    
                     ribbonText.setText("MainRibbonViewBeforeClick");  

                     LayoutInflater inflater = (LayoutInflater)getSystemService     (context.Layout_Inflater_service);   

                    addRow(rootLayout);  
             }  
            }

    /*Here is the calling method in same class which adds one more text view, if we click on text view the ribbon text of oncreate method has to be changed*/ 


      public void addRow(FrameLayout rootLayout)
           {
          Rect rect = new Rect();
          rootLayout.getDrawingRect(rect);
          FrameLayout ribbonLayout = (FrameLayout)findViewById(R.id.subRibbonLayout);
          TextView subText = (TextView)findViewById(R.id.subribbonLayoutText);
          subText.setText("ClicktoChangeTheRibbon");
          subText.setOnclickListener(new onClickListener()  **//Setting up the listener 
         {   
         public vid onclick(View v){     
          FrameLayout ribbonLayout = (FrameLayout)findViewById(R.id.mainribbonLayout);   

                     TextView ribbonText = (TextView)findViewById(R.id.mainribbonLayoutText);    
                     ribbonText.setText("SubRibbonAfterClick");  


                  /*Here I need to implement a code that changes the ribbon text which I have added in onCreate method*/  
                     }  
                });  
4

1 回答 1

0

你应该创建

FrameLayout ribbonLayout 
TextView ribbonText 

在 public void onCreate(Bundle savedInstanceState) 行上方。它将为整个活动创建可变生命周期。因此,您可以在任何可以访问它的地方进行活动。

目前您创建的对象仅适用于 onCreate 方法。

于 2013-09-03T14:42:07.560 回答