显示期间出现Android AlertDialog错误

时间:2017-01-27 13:21:19

标签: android android-alertdialog floating-action-button

我正在开发一个Android项目,此时,我想要这个' fab'以onclick的方式,它带有一个带有文本输入的弹出窗口。和两个按钮。 但它带来了我试图解决的错误,最后我认为我可以在这里找到帮助。

此行有错误:

  

AlertDialog.Builder builder = new AlertDialog.Builder(this);有了这个   一个也是:final EditText input = new EditText(this);

 private String m_Text = "";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("QuickSearch:");
            //input initial
            final EditText input = new EditText(this);
            //
            input.setInputType(InputType.TYPE_CLASS_TEXT );
            builder.setView(input);
            // Set of  buttons to be displayed.
            builder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    m_Text = input.getText().toString();
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            builder.show();


            //Search action here.
            //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                  //  .setAction("Action", null).show();
        }
    });

2 个答案:

答案 0 :(得分:0)

您正在每次按下按钮时创建新的Alertdialog实例..而不删除视图

试试这个:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
   final AlertDialog alert = builder.create();


  fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            builder.setTitle("QuickSearch:");
            //input initial
            final EditText input = new EditText(MainActivity.this);
            //
            input.setInputType(InputType.TYPE_CLASS_TEXT );
            builder.setView(input);
            // Set of  buttons to be displayed.
            builder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    m_Text = input.getText().toString();
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    alert.dismiss();
                }
            });

            alert.show();


            //Search action here.
            //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                  //  .setAction("Action", null).show();
        }
    });

答案 1 :(得分:0)

在您的代码中this指的是onClick而不是context of that Activity

代码应该是这样的

AlertDialog.Builder builder = new AlertDialog.Builder(YourActivityName.this);
final EditText input = new EditText(YourActivityName.this);