我使用自定义DialogFragment和界面以及将点击事件传递回对话框主机(MainActivity)的自定义布局创建了一个对话框。但是我的问题是,当我尝试在对话框中的EditText中输入文本时,它会以空字符串形式返回。有任何想法吗?我可以从对话框中的TextView中获取文本,而不是EditText。
add_class_dialog.xml:
/*
© 2017-present Harald Rudell <harald.rudell@gmail.com> (http://www.haraldrudell.com)
All rights reserved.
*/
import React, {Component} from 'react'
import {bound} from 'class-bind'
const m = 'Form'
export default class Parent extends Component {
state = {one: 'One', two: 'Two'}
@bound submit(e) {
e.preventDefault()
const values = {...this.state}
console.log(`${m}.submit:`, values)
}
@bound fieldUpdate({name, value}) {
this.setState({[name]: value})
}
render() {
console.log(`${m}.render`)
const {state, fieldUpdate, submit} = this
const p = {fieldUpdate}
return (
<form onSubmit={submit}> {/* loop removed for clarity */}
<Child name='one' value={state.one} {...p} />
<Child name='two' value={state.two} {...p} />
<input type="submit" />
</form>
)
}
}
class Child extends Component {
value = this.props.value
@bound update(e) {
const {value} = e.target
const {name, fieldUpdate} = this.props
fieldUpdate({name, value})
}
shouldComponentUpdate(nextProps) {
const {value} = nextProps
const doRender = value !== this.value
if (doRender) this.value = value
return doRender
}
render() {
console.log(`Child${this.props.name}.render`)
const {value} = this.props
const p = {value}
return <input {...p} onChange={this.update} />
}
}
MainActivity.java:
<?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>
AddClassDialogFragment.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();
}
}
答案 0 :(得分:1)
获得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();
}
}
答案 1 :(得分:0)
您刚刚为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();
}
}