2012-08-07 77 views
2

我正在使用Android应用程序。在我的应用程序中,我必须更改按钮点击片段的视图。下面是我的代码:更改按钮单击片段的视图

public View onCreateView(final LayoutInflater inflater, final ViewGroup container, 
     Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 

    if (container == null) { 
     return null; 
    } 
LinearLayout theLayout = (LinearLayout)inflater.inflate(R.layout.settings, container, false); 
Button edit = (Button) theLayout.findViewById(R.id.btn_edit); 
edit.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
// Here I want to change the view.[xml file] 
} 
     }); 
return theLayout; 

} 

在活动,我们可以通过使用setContentView()改变视图。我该如何做到这一点?

+0

http://developer.android.com/reference/android/app/Fragment.html#getView()是指该链接 – Thiru 2012-08-07 10:55:34

回答

2

我找到了我的问题的答案。其实很愚蠢的doupt。

edit.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       /** 
       * Edit button click will replace Settings screen to 
       * edit screen 
       **/ 

       theLayout = (LinearLayout)inflater.inflate(R.layout.edit_settings, container, false); 


      } 
     }); 
0

认为最好在按钮单击时使用两个片段并用另一个片段替换一个片段。如果你想添加片段dinamically - 看到这个例子: http://notionink.wikidot.com/rajeshbabu

+0

也看看这个http://stackoverflow.com/questions/5907673/android-cant-replace-one-fragment-with-another – Saasha 2012-08-07 11:09:23

+0

@Saasha ...我使用tabview这样的四个片段。一个片段是需要两个视图编辑和保存...如果我点击编辑按钮的片段编辑布局将来到编辑后,如果我点击保存然后它将保存并保存按钮将更改为编辑按钮 – sarath 2012-08-07 11:14:27