EditText setText未显示在Dialog Fragment上

时间:2016-07-07 01:31:57

标签: java android android-edittext

我是Android应用开发的新手,并且不知道如何调试它。所以我试图在DialogFragment上做EditText.setText。它在日志上打印出正确的字符串,但仍显示基于.xml文件的旧字符串("在此处输入项目。")。有没有办法更新它?

abstract class RandomMagicHelper(seed: Int) extends { val rand = new Random(seed) } with Magical(rand.nextInt) { thisSuite: Suite =>

  override protected def runTest(testName: String, args: Args): Status = thisSuite.runTest(testName, args)
}

abstract class RandomMagic(seed: Int) extends RandomMagicHelper(seed) with BeforeAndAfterEach { thisSuite: Suite =>

  override protected def runTest(testName: String, args: Args): Status = thisSuite.runTest(testName, args)

  var result = false

  override protected def beforeEach(): Unit = {
    result = true
    super.beforeEach()
  }
}

}

日志文件

public class MyAlertDialogFragment extends DialogFragment{
public MyAlertDialogFragment() {}

public static MyAlertDialogFragment newInstance(String desc) {
    MyAlertDialogFragment frag = new MyAlertDialogFragment();
    Bundle args = new Bundle();
    args.putString("desc", desc);
    frag.setArguments(args);
    return frag;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
    alertDialogBuilder.setView(R.layout.custom_dialog);
    alertDialogBuilder.setTitle("Edit to-do list");

    final View layout = View.inflate(MainActivity.getAppContext(), R.layout.custom_dialog, null);
    final EditText mEditText = (EditText) layout.findViewById(R.id.EditDesc);
    String desc = getArguments().getString("desc");
    mEditText.setText(desc);
    System.out.println("EditText is " + mEditText.getText().toString());

    alertDialogBuilder.setPositiveButton("OK",  new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // on success
            EditTodoFragment.EditTodoFragmentListener listener = (EditTodoFragment.EditTodoFragmentListener)getActivity();
            listener.onFinishEditDialog(mEditText.getText().toString());
        }
    });
    alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    return alertDialogBuilder.create();
}

xml文件是:          

07-06 21:04:20.738 4115-4115/com.example.light.todolist I/System.out: EditText is Doing grocery

2 个答案:

答案 0 :(得分:3)

您不使用Dialog的布局视图,以便您无法在EditText上看到文本。请在此处更改:

 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
    alertDialogBuilder.setTitle("Edit to-do list");

    final View layout = View.inflate(MainActivity.getAppContext(), R.layout.custom_dialog, null);
    alertDialogBuilder.setView(layout );

答案 1 :(得分:2)

试试这个:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());

LayoutInflater inflater = getActivity().getLayoutInflater();
View customView = inflater.inflate(R.layout.custom_dialog, null);

alertDialogBuilder.setView(customView);
alertDialogBuilder.setTitle("Edit to-do list");

final EditText mEditText = (EditText) customView.findViewById(R.id.EditDesc);
String desc = getArguments().getString("desc");
mEditText.setText(desc);

alertDialogBuilder.setPositiveButton("OK",  new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // on success
        EditTodoFragment.EditTodoFragmentListener listener = (EditTodoFragment.EditTodoFragmentListener)getActivity();
        listener.onFinishEditDialog(mEditText.getText().toString());
    }
});
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
    }
});

return alertDialogBuilder.create();