从AlertDialog更新TextView

时间:2016-10-24 18:56:41

标签: java android alertdialog

我正在尝试在android中使用AlertDialog,特别是我希望用户在EditText字段中输入一些信息,然后一旦完成按OK,并将TextView更新为输入的值。

一切正常,直到我想在用户单击“确定”时更新TextView。现在,当用户单击“确定”时,应用程序会一直崩溃。

非常感谢任何想法。

MainActivity.java

    package com.simple.plain.calendar;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



public class MainActivity extends AppCompatActivity {


    String input = "";
    EditText edit;
    TextView text;


   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       //Declaring the textview and button.
       TextView tv = (TextView) findViewById(R.id.calendar_tv);
       Button but = (Button) findViewById(R.id.surprise);

       //When button is clicked, do the following.
       but.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
              showDialog();
           }
       });
    }


private void showDialog(){
    // Create the Dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

    // Inflate and set the layout for the dialog
    LayoutInflater inflater = MainActivity.this.getLayoutInflater();
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.dialog_signin, null));

    edit = (EditText) findViewById(R.id.username);
    text = (TextView) findViewById(R.id.new_text);

    // Add OK button
    builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // User clicked OK button
            input = edit.getText().toString();
            text.setText(input);
        }
    });


    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // User cancelled the dialog
        }
    });

// Set other dialog properties
    builder.setMessage(R.string.dialog_message)
            .setTitle(R.string.dialog_title);



// Create the AlertDialog
    AlertDialog dialog = builder.create();
    dialog.show();
}


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getGroupId()){

            case R.id.action_settings:
                    return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


}

dialog_signin.xml

<?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">


        <EditText
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:layout_marginBottom="4dp"
            android:hint="@string/username" />
        <EditText
            android:id="@+id/password"
            android:inputType="textPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:layout_marginBottom="16dp"
            android:fontFamily="sans-serif"
            android:hint="@string/password"/>
    </LinearLayout>

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:background="#e0e2e5">

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="200dp" >


    <TextView
        android:text="@string/hello_world"
        android:id="@+id/calendar_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:onClick="FireMissilesDialogFragment"
        android:text="@string/surprise_me"
        android:id="@+id/surprise"
        android:layout_marginTop="20dp"/>

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/new_text"/>

</android.support.v7.widget.CardView>

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

您的EditText为空,因为您试图通过活动布局中的ID找到它,而实际上它位于对话框的视图中。

替换这两行:

builder.setView(inflater.inflate(R.layout.dialog_signin, null));

edit = (EditText) findViewById(R.id.username);

有了这些:

View dialogView = inflater.inflate(R.layout.dialog_signin, null);
builder.setView(dialogView );

edit = (EditText) dialogView .findViewById(R.id.username);