在按钮点击上更改片段的视图

时间:2012-08-07 10:50:04

标签: android android-layout android-fragments

我正在使用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()更改视图。我怎么能在片段中做到这一点?

2 个答案:

答案 0 :(得分: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);


            }
        });

答案 1 :(得分:0)

认为最好使用两个片段并在按钮单击时将一个片段替换为另一个片段。如果你想以dinamically方式添加片段 - 请参阅此示例: http://notionink.wikidot.com/rajeshbabu

相关问题