【问题标题】:Changing view of a fragment on button clicks在按钮单击时更改片段的视图
【发布时间】:2012-08-07 10:50:03
【问题描述】:

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

【问题讨论】:

标签: android android-layout android-fragments


【解决方案1】:

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

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


            }
        });

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • @Saasha...我正在使用四个片段,例如 tabview。一个片段需要两个视图编辑和保存...如果我单击片段编辑布局的编辑按钮,编辑后如果我单击保存,它将保存并保存按钮将更改为编辑按钮
    猜你喜欢
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多