可以从对话框textview获取文本,但无法从对话框edittext获取文本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了可以从对话框textview获取文本,但无法从对话框edittext获取文本相关的知识,希望对你有一定的参考价值。
我使用自定义DialogFragment和界面创建了一个对话框,以及将单击事件传递回对话框主机(MainActivity)的自定义布局。但是我的问题是,当我尝试在对话框中的EditText中输入文本时,它会以空字符串形式返回。有任何想法吗?我能够从对话框中的TextView获取文本,而不是EditText。
add_class_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/viewClassName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Class: "
android:textSize="25sp"
android:textColor="@color/textColor"/>
<EditText
android:id="@+id/editClassName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter class"
android:textSize="25sp"
android:textColor="@color/textColor"
android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>
main activity.Java:
public class MainActivity extends FragmentActivity implements AddClassDialogFragment.AddClassDialogListener {
TextView nameView;
EditText editName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View vi = getLayoutInflater().inflate(R.layout.add_class_dialog, null);
nameView = vi.findViewById(R.id.viewClassName);
editName = vi.findViewById(R.id.editClassCredits);
}
public void addClass(View view) {
AddClassDialogFragment acdf = new AddClassDialogFragment();
acdf.show(getFragmentManager(), "ac");
}
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
Toast.makeText(this, nameView.getText().toString(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, editName.getText().toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onDialogNegativeClick(DialogFragment dialog) {
Toast.makeText(this, "Cancel", Toast.LENGTH_SHORT).show();
}
}
add class dialog fragment.Java:
public class AddClassDialogFragment extends DialogFragment {
public interface AddClassDialogListener {
void onDialogPositiveClick(DialogFragment dialog);
void onDialogNegativeClick(DialogFragment dialog);
}
AddClassDialogListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (AddClassDialogListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.add_class_dialog, null));
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onDialogPositiveClick(AddClassDialogFragment.this);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onDialogNegativeClick(AddClassDialogFragment.this);
}
});
return builder.create();
}
}
答案
您获得TextView值的原因是因为您在布局中对其进行了硬编码。 android:text="Class: "
你的代码有几个问题。如果布局用于对话框,则不应在“活动”中对其进行充气。这是你想要实现它的方式。您无需在“活动”中夸大布局。应该在对话框中夸大布局,并且活动应该不知道该布局具有什么或做了什么。无论活动需要知道什么,都应该通过回调来实现。
package com.soulpatch.stackoverflow;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends FragmentActivity implements AddClassDialogFragment.AddClassDialogListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final AddClassDialogFragment acdf = new AddClassDialogFragment();
acdf.show(getSupportFragmentManager(), "abc");
}
@Override
public void onDialogPositiveClick(TextView textView, EditText editText) {
Toast.makeText(this, textView.getText(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, editText.getText() == null ? "" : editText.getText().toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onDialogNegativeClick(TextView textView, EditText editText) {
Toast.makeText(this, textView.getText(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, editText.getText() == null ? "" : editText.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
DialogFragment将通过您在此处声明的界面定义需要返回活动的内容。我已将其更改为采用TextView和EditText。您可以根据您的要求进行更改。
package com.soulpatch.stackoverflow;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class AddClassDialogFragment extends DialogFragment {
public interface AddClassDialogListener {
void onDialogPositiveClick(TextView textView, EditText editText);
void onDialogNegativeClick(TextView textView, EditText editText);
}
AddClassDialogListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (AddClassDialogListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final LayoutInflater inflater = getActivity().getLayoutInflater();
final LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.add_class_dialog, null);
final TextView textView = linearLayout.findViewById(R.id.viewClassName);
final EditText editText = linearLayout.findViewById(R.id.editClassName);
builder.setView(linearLayout);
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onDialogPositiveClick(textView, editText);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onDialogNegativeClick(textView, editText);
}
});
return builder.create();
}
}
另一答案
您刚刚为Edittext引用了错误的ID。
public class MainActivity extends FragmentActivity implements AddClassDialogFragment.AddClassDialogListener {
TextView nameView;
EditText editName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View vi = getLayoutInflater().inflate(R.layout.add_class_dialog, null);
nameView = vi.findViewById(R.id.viewClassName);
editName = vi.findViewById(R.id.editClassName);
}
public void addClass(View view) {
AddClassDialogFragment acdf = new AddClassDialogFragment();
acdf.show(getFragmentManager(), "ac");
}
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
Toast.makeText(this, nameView.getText().toString(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, editName.getText().toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onDialogNegativeClick(DialogFragment dialog) {
Toast.makeText(this, "Cancel", Toast.LENGTH_SHORT).show();
}
}
以上是关于可以从对话框textview获取文本,但无法从对话框edittext获取文本的主要内容,如果未能解决你的问题,请参考以下文章