我有一個帶有EditText的警告框。
當我嘗試將它與用戶輸入進行比較時,它沒有經過「if」。在EditText上使用if語句
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Number of players: ");
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
它不會從這裏開始工作:
String newString = input.getText().toString();
if(newString.equals("2"))
{
System.out.print("lol");
}
感謝您的幫助!
什麼時候在第二個列表中執行代碼? –
它也onCreate – user3311120
在你想要執行的方法中包裝代碼確定警報按鈕點擊然後調用方法在onClick方法 –