自定义Android对话框中的EditText返回null

时间:2015-11-01 20:08:44

标签: android dialog android-dialogfragment

所以我按照Android开发者网站上的当前指南(http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout)创建了一个自定义对话框,并希望将用户输入从EditText传回MainActivity;但事实证明MainActivity收到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>

谢谢, 约什

1 个答案:

答案 0 :(得分:0)

我会以这种方式更改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()); }

.data

    Prompt1:              .asciiz"\nPlease enter the first string.  "
    Prompt2:              .asciiz"\nPlease enter the second string.  "
    theString:            .asciiz"\nYou typed: \n"
    equalPrompt:          .asciiz"\nThe strings are equal\n"
    notEqualPrompt:       .asciiz"\nThe strings are not equal\n"
    stringLengthPrompt:   .asciiz"\nThe string length is: \n"
    stringAppend:         .asciiz"\nThe appended strings are: "
    stringAppend2:        .asciiz" "
    stringOne:  .space 100
    stringTwo:  .space 100


.text

    main:                           
    li  $t0,0
    jal getStrings
    la  $s4,stringOne
    jal stringLength
    move    $s0,$t0             #copy string one length
    la  $s4,stringTwo               
    jal stringLength
    move    $s1,$t0             #copy string two length
    la  $t5,stringOne
    la  $t6,stringTwo
    jal testEquality

    la  $t5,stringOne
    lb  $t1, 0($t5)
    jal append1

    la  $t6,stringTwo
    lb  $t1, 0($t6)
    jal append2
    j   end



getStrings:             

    li  $v0,4               
    la  $a0, Prompt1
    syscall

    li  $v0,8
    la  $a0, stringOne
    li  $a1, 40 
        syscall
                                #string one has been read in
    li  $v0,4               
    la  $a0, Prompt2
    syscall

    li  $v0,8
    la  $a0, stringTwo
    li  $a1, 40
        syscall 
                                #string two has been read in
    la  $a0,theString               #  "you typed:"
    li  $v0,4
    syscall

    la  $a0,stringOne
    li  $v0,4
    syscall

    la  $a0,stringTwo
    li  $v0,4
    syscall

    jr  $ra

end:    

    li  $v0,10
    syscall              

stringLength:                   #calculate length of strings to allocate memory

    li  $t0,0

stringLength1:

    lb  $t1, 0($s4)
    beqz    $t1,out
    addi    $s4,$s4, 1              #move position
    addi    $t0,$t0, 1              #counter
    j   stringLength1


out:

    la  $a0,stringLengthPrompt              
    li  $v0,4
    syscall

    move    $a0, $t0                #length of string 
    li  $v0,1
    syscall
    jr  $ra

equal:  

    la  $a0, equalPrompt
    li  $v0,4
    syscall

    jr  $ra

notEqual:   

    la  $a0, notEqualPrompt
    li  $v0,4
    syscall

    jr  $ra

testEquality:                       #test if both strings are equal

    lb  $t1, 0($t5)
    lb  $t2, 0($t6)                 
    bne $t1,$t2, notEqual
    addi    $t5,$t5,1                           
    addi    $t6,$t6,1   
    add $t7,$t1,$t2
    beqz    $t7,equal
    j   testEquality                    



append1:    

    add $s2,$s0,$s1

    move    $a0,$s2
    li  $v0,9
    syscall

    move    $t8,$v0
    move    $t9,$v0

append2:

    beqz    $t1,append3
    sb  $t1,0($t8)
    addi    $t5,$t5,1
    addi    $t8,$t8,1
    lb  $t1, 0($t5)
    b   append2

append3:

    beqz    $t1,appendOut
    sb  $t1,0($t9)
    addi    $t6,$t6,1
    addi    $t9,$t9,1
    lb  $t1, 0($t6)
    b   append3

appendOut:    

    li  $v0,4               
    la  $a0, stringAppend
    syscall

    move    $a0, $t9                
    li  $v0,4
    syscall

    jr  $ra