Arvin_ghost
2015-06-09 10:05
采纳率: 52.2%
浏览 6.3k

如何在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();
            }
        });

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • danielinbiti 2015-06-09 10:14
    已采纳
     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
    
    打赏 评论
  • 胖虎 2015-06-09 10:14

    补充一下: .setView(viewDialog)

    打赏 评论
  • 胖虎 2015-06-09 10:14

    噢,你这样写是错的,你现在获取到的这个View (viewDialog), 并不是你加载到AlertDialog中的这个,你应该要在setView()中,把这个viewDialog作为参数传进来,然后去获取那个Edittext才是你所见到的那个Edittext

    打赏 评论

相关推荐 更多相似问题