Android 无法在对话框中创建文本输入?

Android 无法在对话框中创建文本输入?,android,android-layout,Android,Android Layout,我是android编程新手。我试图在按下按钮时弹出一个带有文本输入的对话框。当我点击按钮时,应用程序崩溃。需要帮忙吗 enterLP.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDialog(); ToggleButton youToggled = (ToggleButton)

我是android编程新手。我试图在按下按钮时弹出一个带有文本输入的对话框。当我点击按钮时,应用程序崩溃。需要帮忙吗

enterLP.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showDialog();
            ToggleButton youToggled = (ToggleButton) findViewById(R.id.toggleButton1);
            ToggleButton oppToggled = (ToggleButton) findViewById(R.id.toggleButton2);
            TextView yourLP = (TextView) findViewById(R.id.textView4);
            TextView oppLP = (TextView) findViewById(R.id.textView3);
            int lifePoints;
            if (youToggled.isChecked()){
                lifePoints = Integer.parseInt(yourLP.getText().toString()) - Integer.parseInt(reduceLP);
                yourLP.setText(Integer.toString(lifePoints));
            }
            if (oppToggled.isChecked()){
                lifePoints = Integer.parseInt(oppLP.getText().toString()) - Integer.parseInt(reduceLP);
                oppLP.setText(Integer.toString(lifePoints));
            }
        }

    });     


 public void showDialog(){
    AlertDialog.Builder alert = new AlertDialog.Builder(this);  

    alert.setTitle("Subtract Life Points:");  

    final EditText input = new EditText(this); 
    input.setInputType(InputType.TYPE_CLASS_NUMBER);
    alert.setView(input);  

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int whichButton) {  
        reduceLP = input.getText().toString();
      }  
    });  

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

    alert.show();  
}
LogCat错误:

enterLP.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showDialog();
            ToggleButton youToggled = (ToggleButton) findViewById(R.id.toggleButton1);
            ToggleButton oppToggled = (ToggleButton) findViewById(R.id.toggleButton2);
            TextView yourLP = (TextView) findViewById(R.id.textView4);
            TextView oppLP = (TextView) findViewById(R.id.textView3);
            int lifePoints;
            if (youToggled.isChecked()){
                lifePoints = Integer.parseInt(yourLP.getText().toString()) - Integer.parseInt(reduceLP);
                yourLP.setText(Integer.toString(lifePoints));
            }
            if (oppToggled.isChecked()){
                lifePoints = Integer.parseInt(oppLP.getText().toString()) - Integer.parseInt(reduceLP);
                oppLP.setText(Integer.toString(lifePoints));
            }
        }

    });     


 public void showDialog(){
    AlertDialog.Builder alert = new AlertDialog.Builder(this);  

    alert.setTitle("Subtract Life Points:");  

    final EditText input = new EditText(this); 
    input.setInputType(InputType.TYPE_CLASS_NUMBER);
    alert.setView(input);  

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int whichButton) {  
        reduceLP = input.getText().toString();
      }  
    });  

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

    alert.show();  
}
05-12 01:59:30.002: D/dalvikvm(996): GC_FOR_ALLOC freed 54K, 8% free 2458K/2656K, paused 82ms, total 87ms
05-12 01:59:30.011: I/dalvikvm-heap(996): Grow heap (frag case) to 3.157MB for 650176-byte allocation
05-12 01:59:30.102: D/dalvikvm(996): GC_FOR_ALLOC freed 2K, 7% free 3090K/3292K, paused 82ms, total 83ms
05-12 01:59:30.212: D/dalvikvm(996): GC_CONCURRENT freed <1K, 7% free 3090K/3292K, paused 13ms+9ms, total 111ms
05-12 01:59:30.723: D/dalvikvm(996): GC_CONCURRENT freed 646K, 22% free 2849K/3640K, paused 18ms+23ms, total 182ms
05-12 01:59:31.272: D/gralloc_goldfish(996): Emulator without GPU emulation detected.
05-12 01:59:47.924: D/dalvikvm(996): GC_CONCURRENT freed 47K, 11% free 3251K/3640K, paused 75ms+108ms, total 305ms
05-12 01:59:48.021: D/AndroidRuntime(996): Shutting down VM
05-12 01:59:48.021: W/dalvikvm(996): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-12 01:59:48.062: E/AndroidRuntime(996): FATAL EXCEPTION: main
05-12 01:59:48.062: E/AndroidRuntime(996): java.lang.NumberFormatException: Invalid int: ""
05-12 01:59:48.062: E/AndroidRuntime(996):  at java.lang.Integer.invalidInt(Integer.java:138)
05-12 01:59:48.062: E/AndroidRuntime(996):  at java.lang.Integer.parseInt(Integer.java:359)
05-12 01:59:48.062: E/AndroidRuntime(996):  at java.lang.Integer.parseInt(Integer.java:332)
05-12 01:59:48.062: E/AndroidRuntime(996):  at com.aksynial.ygolpcalc.MainActivity$11.onClick(MainActivity.java:232)
05-12 01:59:48.062: E/AndroidRuntime(996):  at android.view.View.performClick(View.java:4204)
05-12 01:59:48.062: E/AndroidRuntime(996):  at android.view.View$PerformClick.run(View.java:17355)
05-12 01:59:48.062: E/AndroidRuntime(996):  at android.os.Handler.handleCallback(Handler.java:725)
05-12 01:59:48.062: E/AndroidRuntime(996):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-12 01:59:48.062: E/AndroidRuntime(996):  at android.os.Looper.loop(Looper.java:137)
05-12 01:59:48.062: E/AndroidRuntime(996):  at android.app.ActivityThread.main(ActivityThread.java:5041)
05-12 01:59:48.062: E/AndroidRuntime(996):  at java.lang.reflect.Method.invokeNative(Native Method)
05-12 01:59:48.062: E/AndroidRuntime(996):  at java.lang.reflect.Method.invoke(Method.java:511)
05-12 01:59:48.062: E/AndroidRuntime(996):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-12 01:59:48.062: E/AndroidRuntime(996):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-12 01:59:48.062: E/AndroidRuntime(996):  at dalvik.system.NativeStart.main(Native Method)
05-12 02:04:48.231: I/Process(996): Sending signal. PID: 996 SIG: 9
05-12 01:59:30.002:D/dalvikvm(996):全部释放54K,8%释放2458K/2656K,暂停82ms,总计87ms
05-12 01:59:30.011:I/dalvikvm堆(996):对于650176字节分配,将堆(frag案例)增长到3.157MB
05-12 01:59:30.102:D/dalvikvm(996):释放2K的所有元素的GC_,7%的自由元素3090K/3292K,暂停82毫秒,总计83毫秒

05-12 01:59:30.212:D/dalvikvm(996):GC_CONCURRENT freed在使用
parseInt
函数之前,不检查提供的变量是否为空

enterLP.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showDialog();
            ToggleButton youToggled = (ToggleButton) findViewById(R.id.toggleButton1);
            ToggleButton oppToggled = (ToggleButton) findViewById(R.id.toggleButton2);
            TextView yourLP = (TextView) findViewById(R.id.textView4);
            TextView oppLP = (TextView) findViewById(R.id.textView3);
            int lifePoints;
            if (youToggled.isChecked()){
                lifePoints = Integer.parseInt(yourLP.getText().toString()) - Integer.parseInt(reduceLP);
                yourLP.setText(Integer.toString(lifePoints));
            }
            if (oppToggled.isChecked()){
                lifePoints = Integer.parseInt(oppLP.getText().toString()) - Integer.parseInt(reduceLP);
                oppLP.setText(Integer.toString(lifePoints));
            }
        }

    });     


 public void showDialog(){
    AlertDialog.Builder alert = new AlertDialog.Builder(this);  

    alert.setTitle("Subtract Life Points:");  

    final EditText input = new EditText(this); 
    input.setInputType(InputType.TYPE_CLASS_NUMBER);
    alert.setView(input);  

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int whichButton) {  
        reduceLP = input.getText().toString();
      }  
    });  

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

    alert.show();  
}
您应该修改代码以防止出现这种情况,您得到的错误只是解释了在这种情况下,值为空,因此函数无法从中返回整数

enterLP.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showDialog();
            ToggleButton youToggled = (ToggleButton) findViewById(R.id.toggleButton1);
            ToggleButton oppToggled = (ToggleButton) findViewById(R.id.toggleButton2);
            TextView yourLP = (TextView) findViewById(R.id.textView4);
            TextView oppLP = (TextView) findViewById(R.id.textView3);
            int lifePoints;
            if (youToggled.isChecked()){
                lifePoints = Integer.parseInt(yourLP.getText().toString()) - Integer.parseInt(reduceLP);
                yourLP.setText(Integer.toString(lifePoints));
            }
            if (oppToggled.isChecked()){
                lifePoints = Integer.parseInt(oppLP.getText().toString()) - Integer.parseInt(reduceLP);
                oppLP.setText(Integer.toString(lifePoints));
            }
        }

    });     


 public void showDialog(){
    AlertDialog.Builder alert = new AlertDialog.Builder(this);  

    alert.setTitle("Subtract Life Points:");  

    final EditText input = new EditText(this); 
    input.setInputType(InputType.TYPE_CLASS_NUMBER);
    alert.setView(input);  

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int whichButton) {  
        reduceLP = input.getText().toString();
      }  
    });  

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

    alert.show();  
}

您应该调试并检查这3个变量的值:
reduceLP
yourLP.getText().toString()
opLP.getText().toString()

,从catlog可以看出您正在解析非整数值,所以Numberformat异常

enterLP.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showDialog();
            ToggleButton youToggled = (ToggleButton) findViewById(R.id.toggleButton1);
            ToggleButton oppToggled = (ToggleButton) findViewById(R.id.toggleButton2);
            TextView yourLP = (TextView) findViewById(R.id.textView4);
            TextView oppLP = (TextView) findViewById(R.id.textView3);
            int lifePoints;
            if (youToggled.isChecked()){
                lifePoints = Integer.parseInt(yourLP.getText().toString()) - Integer.parseInt(reduceLP);
                yourLP.setText(Integer.toString(lifePoints));
            }
            if (oppToggled.isChecked()){
                lifePoints = Integer.parseInt(oppLP.getText().toString()) - Integer.parseInt(reduceLP);
                oppLP.setText(Integer.toString(lifePoints));
            }
        }

    });     


 public void showDialog(){
    AlertDialog.Builder alert = new AlertDialog.Builder(this);  

    alert.setTitle("Subtract Life Points:");  

    final EditText input = new EditText(this); 
    input.setInputType(InputType.TYPE_CLASS_NUMBER);
    alert.setView(input);  

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int whichButton) {  
        reduceLP = input.getText().toString();
      }  
    });  

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

    alert.show();  
}
关于@codeMagic cooment,以下是您可以检查的方法

enterLP.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showDialog();
            ToggleButton youToggled = (ToggleButton) findViewById(R.id.toggleButton1);
            ToggleButton oppToggled = (ToggleButton) findViewById(R.id.toggleButton2);
            TextView yourLP = (TextView) findViewById(R.id.textView4);
            TextView oppLP = (TextView) findViewById(R.id.textView3);
            int lifePoints;
            if (youToggled.isChecked()){
                lifePoints = Integer.parseInt(yourLP.getText().toString()) - Integer.parseInt(reduceLP);
                yourLP.setText(Integer.toString(lifePoints));
            }
            if (oppToggled.isChecked()){
                lifePoints = Integer.parseInt(oppLP.getText().toString()) - Integer.parseInt(reduceLP);
                oppLP.setText(Integer.toString(lifePoints));
            }
        }

    });     


 public void showDialog(){
    AlertDialog.Builder alert = new AlertDialog.Builder(this);  

    alert.setTitle("Subtract Life Points:");  

    final EditText input = new EditText(this); 
    input.setInputType(InputType.TYPE_CLASS_NUMBER);
    alert.setView(input);  

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int whichButton) {  
        reduceLP = input.getText().toString();
      }  
    });  

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

    alert.show();  
}
public boolean isNumeric(String str) {
    int ascii = 0;
    boolean isNumeric = false;
    for (int i = 0; i < str.length(); i++) {
        ascii = (int) str.charAt(i);
        if ((ascii >= 48 && ascii <= 57)) {
            isNumeric = true;
            if (isNumeric) {
                return isNumeric;
            }
        }
    }
    return isNumeric;
}
这两条线-

enterLP.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showDialog();
            ToggleButton youToggled = (ToggleButton) findViewById(R.id.toggleButton1);
            ToggleButton oppToggled = (ToggleButton) findViewById(R.id.toggleButton2);
            TextView yourLP = (TextView) findViewById(R.id.textView4);
            TextView oppLP = (TextView) findViewById(R.id.textView3);
            int lifePoints;
            if (youToggled.isChecked()){
                lifePoints = Integer.parseInt(yourLP.getText().toString()) - Integer.parseInt(reduceLP);
                yourLP.setText(Integer.toString(lifePoints));
            }
            if (oppToggled.isChecked()){
                lifePoints = Integer.parseInt(oppLP.getText().toString()) - Integer.parseInt(reduceLP);
                oppLP.setText(Integer.toString(lifePoints));
            }
        }

    });     


 public void showDialog(){
    AlertDialog.Builder alert = new AlertDialog.Builder(this);  

    alert.setTitle("Subtract Life Points:");  

    final EditText input = new EditText(this); 
    input.setInputType(InputType.TYPE_CLASS_NUMBER);
    alert.setView(input);  

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int whichButton) {  
        reduceLP = input.getText().toString();
      }  
    });  

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

    alert.show();  
}
lifePoints = Integer.parseInt(yourLP.getText().toString()) - Integer.parseInt(reduceLP);
                yourLP.setText(Integer.toString(lifePoints));

lifePoints = Integer.parseInt(oppLP.getText().toString()) - Integer.parseInt(reduceLP);
                oppLP.setText(Integer.toString(lifePoints));
更改为-

enterLP.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showDialog();
            ToggleButton youToggled = (ToggleButton) findViewById(R.id.toggleButton1);
            ToggleButton oppToggled = (ToggleButton) findViewById(R.id.toggleButton2);
            TextView yourLP = (TextView) findViewById(R.id.textView4);
            TextView oppLP = (TextView) findViewById(R.id.textView3);
            int lifePoints;
            if (youToggled.isChecked()){
                lifePoints = Integer.parseInt(yourLP.getText().toString()) - Integer.parseInt(reduceLP);
                yourLP.setText(Integer.toString(lifePoints));
            }
            if (oppToggled.isChecked()){
                lifePoints = Integer.parseInt(oppLP.getText().toString()) - Integer.parseInt(reduceLP);
                oppLP.setText(Integer.toString(lifePoints));
            }
        }

    });     


 public void showDialog(){
    AlertDialog.Builder alert = new AlertDialog.Builder(this);  

    alert.setTitle("Subtract Life Points:");  

    final EditText input = new EditText(this); 
    input.setInputType(InputType.TYPE_CLASS_NUMBER);
    alert.setView(input);  

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int whichButton) {  
        reduceLP = input.getText().toString();
      }  
    });  

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

    alert.show();  
}
if(yourLP.getText().toString().matches("\\d+") || reduceLP..matches("\\d+") )
{
lifePoints = Integer.parseInt(yourLP.getText().toString()) - Integer.parseInt(reduceLP);
                    yourLP.setText(Integer.toString(lifePoints));
}

if(yourLP.getText().toString().matches("\\d+") || reduceLP..matches("\\d+"))
{
lifePoints = Integer.parseInt(oppLP.getText().toString()) - Integer.parseInt(reduceLP);
                    yourLP.setText(Integer.toString(lifePoints));
}

只需检查
yourLP
oppLP
reduceLP
是否包含数字。

您可能应该做这两件事来简化代码并避免异常

enterLP.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showDialog();
            ToggleButton youToggled = (ToggleButton) findViewById(R.id.toggleButton1);
            ToggleButton oppToggled = (ToggleButton) findViewById(R.id.toggleButton2);
            TextView yourLP = (TextView) findViewById(R.id.textView4);
            TextView oppLP = (TextView) findViewById(R.id.textView3);
            int lifePoints;
            if (youToggled.isChecked()){
                lifePoints = Integer.parseInt(yourLP.getText().toString()) - Integer.parseInt(reduceLP);
                yourLP.setText(Integer.toString(lifePoints));
            }
            if (oppToggled.isChecked()){
                lifePoints = Integer.parseInt(oppLP.getText().toString()) - Integer.parseInt(reduceLP);
                oppLP.setText(Integer.toString(lifePoints));
            }
        }

    });     


 public void showDialog(){
    AlertDialog.Builder alert = new AlertDialog.Builder(this);  

    alert.setTitle("Subtract Life Points:");  

    final EditText input = new EditText(this); 
    input.setInputType(InputType.TYPE_CLASS_NUMBER);
    alert.setView(input);  

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int whichButton) {  
        reduceLP = input.getText().toString();
      }  
    });  

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

    alert.show();  
}
  • 导致异常的空字符串上的parseInt,因此请检查是否为空,如上面的答案中所述

  • enterLP.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                showDialog();
                ToggleButton youToggled = (ToggleButton) findViewById(R.id.toggleButton1);
                ToggleButton oppToggled = (ToggleButton) findViewById(R.id.toggleButton2);
                TextView yourLP = (TextView) findViewById(R.id.textView4);
                TextView oppLP = (TextView) findViewById(R.id.textView3);
                int lifePoints;
                if (youToggled.isChecked()){
                    lifePoints = Integer.parseInt(yourLP.getText().toString()) - Integer.parseInt(reduceLP);
                    yourLP.setText(Integer.toString(lifePoints));
                }
                if (oppToggled.isChecked()){
                    lifePoints = Integer.parseInt(oppLP.getText().toString()) - Integer.parseInt(reduceLP);
                    oppLP.setText(Integer.toString(lifePoints));
                }
            }
    
        });     
    
    
     public void showDialog(){
        AlertDialog.Builder alert = new AlertDialog.Builder(this);  
    
        alert.setTitle("Subtract Life Points:");  
    
        final EditText input = new EditText(this); 
        input.setInputType(InputType.TYPE_CLASS_NUMBER);
        alert.setView(input);  
    
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
        public void onClick(DialogInterface dialog, int whichButton) {  
            reduceLP = input.getText().toString();
          }  
        });  
    
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {  
         public void onClick(DialogInterface dialog, int whichButton) {  
              dialog.cancel();
          }  
        });  
    
        alert.show();  
    }
    
  • 始终使用布局工具为对话框创建自定义视图,以便

  • enterLP.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                showDialog();
                ToggleButton youToggled = (ToggleButton) findViewById(R.id.toggleButton1);
                ToggleButton oppToggled = (ToggleButton) findViewById(R.id.toggleButton2);
                TextView yourLP = (TextView) findViewById(R.id.textView4);
                TextView oppLP = (TextView) findViewById(R.id.textView3);
                int lifePoints;
                if (youToggled.isChecked()){
                    lifePoints = Integer.parseInt(yourLP.getText().toString()) - Integer.parseInt(reduceLP);
                    yourLP.setText(Integer.toString(lifePoints));
                }
                if (oppToggled.isChecked()){
                    lifePoints = Integer.parseInt(oppLP.getText().toString()) - Integer.parseInt(reduceLP);
                    oppLP.setText(Integer.toString(lifePoints));
                }
            }
    
        });     
    
    
     public void showDialog(){
        AlertDialog.Builder alert = new AlertDialog.Builder(this);  
    
        alert.setTitle("Subtract Life Points:");  
    
        final EditText input = new EditText(this); 
        input.setInputType(InputType.TYPE_CLASS_NUMBER);
        alert.setView(input);  
    
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
        public void onClick(DialogInterface dialog, int whichButton) {  
            reduceLP = input.getText().toString();
          }  
        });  
    
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {  
         public void onClick(DialogInterface dialog, int whichButton) {  
              dialog.cancel();
          }  
        });  
    
        alert.show();  
    }
    
    AlertDialog.Builder save_dialog=新建AlertDialog.Builder(此)

    enterLP.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                showDialog();
                ToggleButton youToggled = (ToggleButton) findViewById(R.id.toggleButton1);
                ToggleButton oppToggled = (ToggleButton) findViewById(R.id.toggleButton2);
                TextView yourLP = (TextView) findViewById(R.id.textView4);
                TextView oppLP = (TextView) findViewById(R.id.textView3);
                int lifePoints;
                if (youToggled.isChecked()){
                    lifePoints = Integer.parseInt(yourLP.getText().toString()) - Integer.parseInt(reduceLP);
                    yourLP.setText(Integer.toString(lifePoints));
                }
                if (oppToggled.isChecked()){
                    lifePoints = Integer.parseInt(oppLP.getText().toString()) - Integer.parseInt(reduceLP);
                    oppLP.setText(Integer.toString(lifePoints));
                }
            }
    
        });     
    
    
     public void showDialog(){
        AlertDialog.Builder alert = new AlertDialog.Builder(this);  
    
        alert.setTitle("Subtract Life Points:");  
    
        final EditText input = new EditText(this); 
        input.setInputType(InputType.TYPE_CLASS_NUMBER);
        alert.setView(input);  
    
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
        public void onClick(DialogInterface dialog, int whichButton) {  
            reduceLP = input.getText().toString();
          }  
        });  
    
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {  
         public void onClick(DialogInterface dialog, int whichButton) {  
              dialog.cancel();
          }  
        });  
    
        alert.show();  
    }
    

    希望它能帮助您

    请查看此链接。这可能会有帮助+回答得好。但是OP应该注意,不仅要检查空值,还要确保它是一个
    int
    ,以防用户输入字母、空格、特殊字符等。。。
    enterLP.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                showDialog();
                ToggleButton youToggled = (ToggleButton) findViewById(R.id.toggleButton1);
                ToggleButton oppToggled = (ToggleButton) findViewById(R.id.toggleButton2);
                TextView yourLP = (TextView) findViewById(R.id.textView4);
                TextView oppLP = (TextView) findViewById(R.id.textView3);
                int lifePoints;
                if (youToggled.isChecked()){
                    lifePoints = Integer.parseInt(yourLP.getText().toString()) - Integer.parseInt(reduceLP);
                    yourLP.setText(Integer.toString(lifePoints));
                }
                if (oppToggled.isChecked()){
                    lifePoints = Integer.parseInt(oppLP.getText().toString()) - Integer.parseInt(reduceLP);
                    oppLP.setText(Integer.toString(lifePoints));
                }
            }
    
        });     
    
    
     public void showDialog(){
        AlertDialog.Builder alert = new AlertDialog.Builder(this);  
    
        alert.setTitle("Subtract Life Points:");  
    
        final EditText input = new EditText(this); 
        input.setInputType(InputType.TYPE_CLASS_NUMBER);
        alert.setView(input);  
    
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
        public void onClick(DialogInterface dialog, int whichButton) {  
            reduceLP = input.getText().toString();
          }  
        });  
    
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {  
         public void onClick(DialogInterface dialog, int whichButton) {  
              dialog.cancel();
          }  
        });  
    
        alert.show();  
    }