2016-07-07 74 views
5

我對Android應用程序開發並不熟悉,也不知道如何進行調試。所以我試圖在DialogFragment上做EditText.setText。它在日誌中打印出正確的字符串,但仍然顯示基於.xml文件的舊字符串(「在此處輸入項目」)。有什麼方法可以更新嗎?EditText setText不在對話框片段上顯示

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(); 
} 

}

日誌文件

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

XML文件是:

<TextView 
    android:layout_width="238dp" 
    android:layout_height="wrap_content" 
    android:text="Edit Item Below:" 
    android:id="@+id/EditTitle" 
    android:layout_weight="0.04" /> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/EditDesc" 
    android:text="Enter item here."> 
<requestFocus /> 
</EditText> 

回答

3

你不ü用於對話框的佈局視圖,以便在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); 
+0

你說得對!我必須使用佈局來設置視圖,否則,它不會識別它? – user3315093

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();