2017-03-04 17 views
1

我有一個應用程序,我想在其中調用一個打開帶有數字輸入的對話框的方法,我的問題是隻要在edittext中插入一個值並按下正向按鈕它會返回一個空值,然後在下一次調用時先返回值。帶edittext的警報對話框返回null,因爲它在賦值之前返回

通過調試,我發現它創建了元素,然後返回跳過我的賦值語句。

我該如何防止它返回太早?

private String numbers; 
    private String getWeight(){ 
     final EditText edittext = new EditText(this); 
     edittext.setInputType(InputType.TYPE_CLASS_PHONE); 
     AlertDialog.Builder builder1 = new AlertDialog.Builder(this); 
     builder1.setMessage("How much do you weigh?"); 
     builder1.setView(edittext); 
     builder1.setCancelable(true); 

     builder1.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 

        numbers = edittext.getText().toString(); 
       dialog.cancel(); 

      } 
     }); 

     builder1.setNeutralButton(
       "Cancel", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         numbers = edittext.getText().toString(); 
         dialog.cancel(); 
        } 
       }); 

     AlertDialog alert11 = builder1.create(); 
     alert11.show(); 

     return numbers; 
    } 
+0

你不需要從你的函數返回「數字」,因爲我從你的代碼,發現了「數字」是一個全局變量。 – pooyan

回答

0

因此在鍵入時在字符串中輸入editText內容。

String content; 
editText.addTextChangedListener(new TextWatcher() { 

@Override 
public void onTextChanged(CharSequence s, int start, int before, int count) { 
// TODO Auto-generated method stub 
    content = s.toString(); 
} 

@Override 
public void beforeTextChanged(CharSequence s, int start, int count,int after) { 
// TODO Auto-generated method stub 

} 

@Override 
public void afterTextChanged(Editable s) { 
if(TextUtils.isEmpty(s)) { 
// call method 
} 
} 
}); 

和setPositiveButton在這裏獲取內容字符串。

0

我有一個相同的問題,並使用以下方法修復它。

您應該爲視圖創建一個靜態xml佈局。不要以編程方式構建它。

更新代碼

private String numbers; 
private String getWeight(){ 
    LayoutInflater inflater = LayoutInflater.from(mContext); 
    View mView = inflater.inflate(R.layout.weight_simple_input, null); 
    builder.setView(mView); 
    AlertDialog.Builder builder1 = new AlertDialog.Builder(this); 
    builder1.setMessage("How much do you weigh?"); 
    builder1.setView(mView); 
    builder1.setCancelable(true); 

    builder1.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
       numbers = ((EditText) mView.findViewById(R.id.phone)).getText().toString(); 
      dialog.cancel(); 

     } 
    }); 

    builder1.setNeutralButton(
      "Cancel", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        numbers = edittext.getText().toString(); 
        dialog.cancel(); 
       } 
      }); 

    AlertDialog alert11 = builder1.create(); 
    alert11.show(); 
} 

而且同時你在類範圍內定義的數字,則不需要再次返回。在任何你想要的地方使用它。

最後這裏是layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

    <EditText 
    android:layout_width="wrap_content" 
    android:layout_gravity="center" 
    android:layout_marginBottom="@dimen/text_margin" 
    android:layout_height="wrap_content" 
    android:inputType="numberDecimal" 
    android:ems="10" 
    android:id="@+id/weight" /> 
</LinearLayout>