【问题标题】:Android custom dialog with button带有按钮的Android自定义对话框
【发布时间】:2012-06-15 08:55:00
【问题描述】:

今天很简单的问题。当我尝试检索对对话框上按钮的引用时,我总是收到一个空值。

@Override
public void onRestart() {
    super.onRestart();


    //must prompt with modal dialog for pin
    Dialog dialog = new Dialog(this);
    dialog.setOwnerActivity(this);
    dialog.setCancelable(false);

    //check pin
    dialog.setCanceledOnTouchOutside(false);
    //set view to enterpin XML screen
    dialog.setContentView(R.layout.enterpin);
    //register button

    //show dialog
    dialog.show();
    //listen for button being clicked
    Button button = (Button) findViewById(R.id.pinlogin);
    button.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
            EditText editText = (EditText) findViewById(R.id.enterpin);
            int enteredPin = Integer.parseInt(editText.getText().toString());
            SharedPreferences sharedP = getPreferences(MODE_PRIVATE);
            int temp = sharedP.getInt("pin", 0);
            if(enteredPin==temp){
                pinCheck = true;
            }else{
                pinCheck = false;
            }
        }
    });
    if(pinCheck){
        dialog.dismiss();
    }



}

是的,按钮存在,不,我没有拼错它的引用。是的,我已经清理了项目并重新启动了 Eclipse。当我清楚地调用 setContentView() 时,按钮如何不与视图关联?我敢肯定这很简单,我以前从未使用过对话框,一般来说是 GUI 新手。尤其是安卓

【问题讨论】:

    标签: java android xml button


    【解决方案1】:
    Button button = (Button)dialog.findViewById(R.id.pinlogin);
                            ^^^^^^
    

    当你显示对话框时,你必须给refrences of dialog。因为没有它,Button refrences from main.xml。我的意思是从主视图setContentView(R.layout.main);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-16
      相关资源
      最近更新 更多