开发者社区> 问答> 正文

如何在dialog中获取edittext的值

代码如下,这样获取edittext.gettext().tostring()值总为空,该怎么改呢?

               final View viewDialog=(View)getLayoutInflater().inflate(R.layout.ad_dialog,null);
                new AlertDialog.Builder(Ad_Delete.this).setTitle("请输入删除数量(不能大于现有本数)")
                        .setView(getLayoutInflater().inflate(R.layout.ad_dialog, null))
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                                EditText ad_delete_edit = (EditText)viewDialog.findViewById(R.id.ad_delete_number);
                                int deleteNumber = Integer.parseInt(ad_delete_edit.getText().toString());

                                if (deleteNumber > nowNumber || deleteNumber < 0) {
                                    Toast.makeText(Ad_Delete.this, "输入数量有误,请输入不大于现有数量的数目", Toast.LENGTH_LONG).show();
                                } else {
                                    sqLiteOpenHelper_ad.getReadableDatabase().execSQL("updata Administrator_inf set book_totalnumber = ?, " +
                                                    "book_nownumber = ? where _id = ?",
                                            new Integer[]{totalNumber - deleteNumber, nowNumber - deleteNumber, id});
                                    Toast.makeText(Ad_Delete.this, "删除成功!", Toast.LENGTH_LONG).show();
                                }
                            }
                        }).show();
            }
        });

展开
收起
爵霸 2016-06-16 09:50:18 2360 0
1 条回答
写回答
取消 提交回答
  •  final View viewDialog=(View)getLayoutInflater().inflate(R.layout.ad_dialog,null);
                    new AlertDialog.Builder(Ad_Delete.this).setTitle("请输入删除数量(不能大于现有本数)")
                            .setView(getLayoutInflater().inflate(R.layout.ad_dialog, null))

    改成

        final View viewDialog=(View)getLayoutInflater().inflate(R.layout.ad_dialog,null);
                    new AlertDialog.Builder(Ad_Delete.this).setTitle("请输入删除数量(不能大于现有本数)")
                            .setView(viewDialog)
    不然你获取edittext的viewDialog不是dialog显示的viewDialog
    
    2019-07-17 19:40:20
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载