如何从AlertDialog显示吐司? [英] How to display Toast from AlertDialog?

查看:354
本文介绍了如何从AlertDialog显示吐司?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上保存用户点击按钮,一个 AlertDialog ,并询问使用输入文本文件名。

如果用户点击了积极的按钮(OK)没有指定名称,我要显示它要求他们这样做敬酒,并保持 AlerDialog 打开。但土司永远不会显示,对话框关闭。

在code为 AlertDialog 是在这里:

  AlertDialog.Builder警报=新AlertDialog.Builder(本);    alert.setTitle(R.string.save_game);
    alert.setMessage(R.string.request_name);    //设置一个EditText视图来获取用户输入
    最终的EditText输入=新的EditText(本);
    input.setHint(R.string.untitled);
    alert.setView(输入);    alert.setPositiveButton(OK,新DialogInterface.OnClickListener(){
    公共无效的onClick(DialogInterface对话,诠释whichButton){
      字符串值= input.getText()的toString()。
      如果(值!= NULL){
          //使用有价值的东西
      }
      其他{
          Toast.makeText(上下文,R.string.no_name_given,Toast.LENGTH_SHORT).show();
      }
    }
    });    alert.setNegativeButton(取消,新DialogInterface.OnClickListener(){
    公共无效的onClick(DialogInterface对话,诠释whichButton){
        // 取消。
    }
    });    alert.show();

我怎样才能做到这一点?

谢谢!


解决方案

 公共无效showToast(){
    Toast.makeText(这一点,R.string.no_name_given,Toast.LENGTH_SHORT).show();
}

就调用此方法,而不是在你的code从这样一个警告对话框,显示敬酒。

 其他{
    ShowToast();
}

要保持开放使用这种方法

 公共无效forceOpen(){AlertDialog.Builder警报=新AlertDialog.Builder(本);alert.setTitle(R.string.save_game);
alert.setMessage(R.string.request_name);//设置一个EditText视图来获取用户输入
最终的EditText输入=新的EditText(本);
input.setHint(R.string.untitled);
alert.setView(输入);alert.setPositiveButton(OK,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释whichButton){
  字符串值= input.getText()的toString()。
  如果(值!= NULL){
      //使用有价值的东西
  }
  其他{
      Toast.makeText(上下文,R.string.no_name_given,Toast.LENGTH_SHORT).show();
  }
}
});alert.setNegativeButton(取消,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释whichButton){
    // 取消。
}
});alert.show();    }

刚刚重新打开它..不知道为什么它会关闭,但这将工作

When the user clicks on the save button, an AlertDialog appears and asks the use to input text for the file name.

If the user clicks the positive button ("Ok") without specifying a name, I want to display a toast which asks them to do so, and keep the AlerDialog open. But the toast never displays and the dialog closes.

The code for the AlertDialog is here:

    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle(R.string.save_game);
    alert.setMessage(R.string.request_name);

    // Set an EditText view to get user input 
    final EditText input = new EditText(this);
    input.setHint(R.string.untitled);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
      String value = input.getText().toString();
      if(value != null){
          // Do something with value      
      }
      else{
          Toast.makeText(context, R.string.no_name_given, Toast.LENGTH_SHORT).show();
      }
    }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // Canceled.
    }
    });

    alert.show();

How can I make this happen?

Thanks!

解决方案

Public void showToast(){
    Toast.makeText(this, R.string.no_name_given, Toast.LENGTH_SHORT).show();
} 

Just call this method instead of displaying toast from an alert dialog box like this in your code.

else{
    ShowToast();
}

To keep it open use this method

public void forceOpen() {

AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle(R.string.save_game);
alert.setMessage(R.string.request_name);

// Set an EditText view to get user input 
final EditText input = new EditText(this);
input.setHint(R.string.untitled);
alert.setView(input);

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
  String value = input.getText().toString();
  if(value != null){
      // Do something with value      
  }
  else{
      Toast.makeText(context, R.string.no_name_given, Toast.LENGTH_SHORT).show();
  }
}
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
    // Canceled.
}
});

alert.show();

    }

Just reopen it.. Not sure why it closes but this will work

这篇关于如何从AlertDialog显示吐司?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆