2015-11-01 62 views
1

因此,我根據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> 

感謝, 喬希

+0

[Custom Dialog EditText返回null](http://stackoverflow.com/questions/10828901/custom-dialog-edittext-returns-null) –

回答

0

我會改變NoticeDialogListener這種方式,因爲你只需要的EditText

public interface NoticeDialogListener { 
    public void onDialogPositiveClick(final String input); 
    public void onDialogNegativeClick(); 
} 

setPositiveButton被調用,

.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      listener.onDialogPositiveClick(((TextVew)view.findViewById(R.id.newitem)).getText().toStrig()); 
     } 
內容
+0

真棒,非常感謝你! –

+0

不客氣 – Blackbelt