获取EditText值显示AlertDialog

时间:2014-01-06 17:47:51

标签: java android

是否可以从EditText获取用户输入文本并将其显示在AlertDialog中?主活动提示用户设置闹钟,然后输入一条消息,以便在闹钟响起后显示。我希望能够显示用户在AlertDialog中键入的任何消息。

MainActivity.java

public class MainActivity extends Activity{

    TimePicker picker;
    DatePicker datepicker;
    EditText editText;

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.settings, menu);
        return true;

    }

    public void setAlarm(View view) {
        picker = (TimePicker)findViewById(R.id.timePicker1);
        datepicker = (DatePicker)findViewById(R.id.datePicker1);

        Calendar AlarmCal = Calendar.getInstance();

        Intent intent = new Intent(this, AlarmBoadcastReceiver.class);
        PendingIntent pendingintent = PendingIntent.getBroadcast(this.getApplicationContext(), 9988, intent, 0);

        AlarmCal.setTimeInMillis(System.currentTimeMillis());
        AlarmCal.set(Calendar.HOUR_OF_DAY, picker.getCurrentHour());
        AlarmCal.set(Calendar.MINUTE, picker.getCurrentMinute());
        AlarmCal.set(Calendar.DATE, datepicker.getDayOfMonth());
        AlarmCal.set(Calendar.YEAR, datepicker.getYear());

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, AlarmCal.getTimeInMillis(), pendingintent);

    }

}

AlarmBroadCastReciever.java

public class AlarmBoadcastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {

        showNotification(context);

        Vibrator vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(1000);

    }

    private void showNotification(Context context) {

        PendingIntent contentintent = PendingIntent.getActivity(context, 0, new Intent(context, ShowDialog.class), 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.ic_notif)
        .setContentTitle("Reminder")
        .setContentText("Click Here To View Message");

        mBuilder.setContentIntent(contentintent);
        mBuilder.setAutoCancel(true);

        NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());

    }

} 

ShowDialog.java

public class ShowDialog extends Activity {

    final Context context = this;

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

        showDialog();

    }

    void showDialog() {

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

        alertDialogBuilder.setTitle("Message");

        alertDialogBuilder
            .setMessage("message")
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ShowDialog.this.finish();
                    closeApp();

                }
            });

        AlertDialog alertDialog = alertDialogBuilder.create();

        alertDialog.show();

    }

}

因此,当单击通知时,AlertDialog会显示,但不显示用户输入的消息。我已经尝试获取值并将它们放在一个字符串中,但这不起作用。任何想法?

3 个答案:

答案 0 :(得分:0)

尝试创建自己的Dialog类扩展的Dialog,并将此消息添加到构造函数。

答案 1 :(得分:0)

将消息作为EXTRA添加到警报pendingIntent:

Intent intent = new Intent(this, AlarmBoadcastReceiver.class);
intent.putExtra("message", yourMessageString);

获取AlarmBoadcastReceiver onReceive方法

中的字符串
yourMessageString = intent.getExtras().getString("message","No message");

showNotification中将该字符串作为EXTRA添加到pendingintent

intent = new Intent(context, ShowDialog.class);    
intent.putExtra("message", yourMessageString);
PendingIntent contentintent = PendingIntent.getActivity(context, 0, intent, 0);  

ShowDialog onCreate方法上获得额外的意图:

userMessage = getIntent().getExtras().getString("message","No message");  

然后将该消息设置为AlertDialog

alertDialogBuilder.setMessage(userMessage)

答案 2 :(得分:0)

我讨厌回答我自己的问题,但我接受了@StarsSky提供的建议,并使用共享偏好设置来存储和检索要在AlertDialog中显示的消息

保存

SharedPreferences.Editor editor = getSharedPreferences(null, MODE_PRIVATE).edit();
        editor.putString("text", editText.getText().toString());
        editor.commit();

<强>接收

SharedPreferences prefs =getSharedPreferences(null, MODE_PRIVATE);
        String text = prefs.getString("text", null);