gpt4 book ai didi

java - 如何在 DialogFragment 上定位 EditText

转载 作者:行者123 更新时间:2023-12-02 05:47:25 24 4
gpt4 key购买 nike

我创建了一个如下所示的 DialogFragment:

package com.name.test;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.name.test.MainActivity.DialogType;

public class AlertDialogFragment extends DialogFragment {

public static DialogType type;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

View v = getActivity().getLayoutInflater().inflate(R.layout.search_fragment, null);

final EditText searchField = (EditText)v.findViewById(R.id.searchEditTxt);

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();

switch(type) {
case SEARCH:

builder.setView(inflater.inflate(R.layout.search_fragment, null))

.setTitle("Search Dialog")
.setPositiveButton("Search", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

// logic for the search button
// Toast.makeText(getActivity().getBaseContext(), "Search Dialog", Toast.LENGTH_SHORT).show();

String tempString = searchField.getText().toString();
System.out.println(tempString);

}
})
//.setView(searchField)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

// logic for the cancel button
AlertDialogFragment.this.getDialog().cancel();
}
});


break;
case PREFERENCES:
builder.setView(inflater.inflate(R.layout.preferences_fragment, null))
.setTitle("Preferences")
.setPositiveButton("Save", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

// logic for the search button
Toast.makeText(getActivity().getBaseContext(), "Preferences", Toast.LENGTH_SHORT).show();

}
})
//.setView(searchField)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

// logic for the cancel button
AlertDialogFragment.this.getDialog().cancel();
}
});


break;

default:
break;

}


return builder.create();

}

public static AlertDialogFragment newInstance(DialogType dialogType) {
type = dialogType;

return new AlertDialogFragment();
}

}

在此 fragment 的 XML 文件中,我有以下内容:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="search_fragment"
android:orientation="vertical" >

<EditText
android:id="@+id/searchEditTxt"
android:hint="@string/search_menu_hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"

/>
</LinearLayout>

我试图在 DialogFragment 中定位该 EditText,以便当用户键入内容并单击“保存”或“确定”时,我可以存储其值。我似乎无法正确定位 EditText。我的System.out.println(tempString);是空的。我究竟做错了什么?非常感谢。

最佳答案

尝试改变

builder.setView(inflater.inflate(R.layout.search_fragment, null));

builder.setView(v);

您只需将您的膨胀 View 设置为您的构建器

关于java - 如何在 DialogFragment 上定位 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23924918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com