因此,我根據Android開發人員網站(http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout)上的當前指南創建了自定義對話框,並希望將用戶輸入從EditText傳回給MainActivity;但事實證明MainActivity接收到null。我看不出爲什麼。定製Android對話框中的EditText返回null
任何幫助,非常感謝。
這裏是我的代碼:
MainActivity.java
public class MainActivity extends AppCompatActivity implements NewItemFragment.NoticeDialogListener {
private final static String TAG = "tag";
RecyclerView recyclerView;
RecyclerView.LayoutManager manager;
RecyclerView.Adapter adapter;
FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NewItemFragment dialog = new NewItemFragment();
dialog.show(getSupportFragmentManager(), "newitem");
}
});
manager = new LinearLayoutManager(this);
recyclerView = (RecyclerView) findViewById(R.id.recycler);
recyclerView.setLayoutManager(manager);
initialiseData();
initialiseAdapter();
}
@Override
public void onDialogPositiveClick(DialogFragment dialogFragment) {
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.new_item_dialog, null);
EditText editText = (EditText) view.findViewById(R.id.newitem);
String input = editText.getText().toString();
EditText result = (EditText) findViewById(R.id.textedit);
int i = entries.size();
if (input.equals("")) {
result.setText("Fail");
} else {
entries.add(new Items(input));
adapter.notifyItemInserted(i + 1);
}
}
@Override
public void onDialogNegativeClick(DialogFragment dialogFragment) {
}
}
NewItemFragment.java
public class NewItemFragment extends DialogFragment {
public interface NoticeDialogListener {
public void onDialogPositiveClick(DialogFragment dialogFragment);
public void onDialogNegativeClick(DialogFragment dialogFragment);
}
NoticeDialogListener listener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
listener = (NoticeDialogListener) 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 View view = inflater.inflate(R.layout.new_item_dialog, null);
builder.setView(view)
.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
listener.onDialogPositiveClick(NewItemFragment.this);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
NewItemFragment.this.getDialog().cancel();
}
});
return builder.create();
}
}
最後new_item_dialog。 XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="24dp"
android:paddingStart="24dp"
android:paddingEnd="24dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="#000000"
android:text="@string/new_item"
android:layout_marginBottom="20dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/newitem"
android:hint="@string/hint"
android:layout_marginBottom="24dp"/>
</LinearLayout>
感謝, 喬希
[Custom Dialog EditText返回null](http://stackoverflow.com/questions/10828901/custom-dialog-edittext-returns-null) –