Java 如何将EditText定位到DialogFragment上
我创建了一个DialogFragment,如下所示:Java 如何将EditText定位到DialogFragment上,java,android,Java,Android,我创建了一个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 an
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();
}
}
在这个片段的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为目标,这样当用户键入内容并单击Save或OK时,我就可以存储其值。我似乎无法正确定位编辑文本。MySystem.out.println(tempString)代码>为空。我做错了什么?多谢 尝试改变
builder.setView(充气器.inflate(R.layout.search_fragment,null))代码>
到
builder.setView(v)代码>
您只需将充气视图
设置为生成器
尝试更改builder.setView(充气器.充气(R.layout.search_fragment,null))代码>到builder.setView(v)代码>成功了,非常感谢。请使用答案选项,这样我可以给你一些信用。