3

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

4

2 回答 2

2

我找到了我的问题的答案。实际上非常愚蠢的怀疑。

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);


            }
        });
于 2012-08-07T11:38:27.330 回答
0

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

于 2012-08-07T11:06:16.630 回答