2016-01-16 27 views
0

我有一個按鈕,當用戶在EditText中輸入一個數字時,它按照它的功能工作。但是,當我的EditText數值爲空時,當我點擊相同的按鈕時,它使我的應用程序停止工作。任何幫助?如何僅在EditText在android studio中數值爲空時禁用按鈕?

請考慮「數字」部分,因爲我無法找到我的查詢的任何答案?

code 


    public void send(View v){ 
    String text = edittext.getText().toString(); 
    if (text.matches("")) { 
myButton.setEnabled(false); 
return; 
     } 
} 

XML

<LinearLayout android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <EditText 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:inputType="number" 
     android:layout_height="wrap_content" 
     android:hint="Enter:" 
     android:id="@+id/edittext" 
     /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Send" 
     android:onClick="send" 
     android:id="@+id/btn"/> 
</LinearLayout> 

<ListView 
    android:id="@+id/listview" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    /> 

logcat 

01-16 10:49:15.208 3014-3014/? I/SELinux: Function: selinux_android_load_priority [0], There is no sepolicy file. 

01-16 10:49:15.208 3014-3014/? I/SELinux: Function: selinux_android_load_priority [1], There is no sepolicy version file. 

01-16 10:49:15.208 3014-3014/? I/SELinux: Function: selinux_android_load_priority , priority version is VE=GOOGLE_POLICY 


01-16 10:49:15.208 3014-3014/? I/SELinux: selinux_android_seapp_context_reload: seapp_contexts file is loaded from /seapp_contexts 
01-16 10:49:15.208 3014-3014/? E/SELinux: [DEBUG] seapp_context_lookup: seinfoCategory = default 
01-16 10:49:15.208 3014-3014/? E/SELinux: [DEBUG] seapp_context_lookup: seinfoCategory = default 
01-16 10:49:15.208 3014-3014/? D/dalvikvm: Late-enabling CheckJNI 
01-16 10:49:15.298 3014-3014/com.example.dell.uomopenday I/dalvikvm: Could not find method android.app.Notification$Builder.setLocalOnly, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza 
01-16 10:49:15.298 3014-3014/com.example.dell.uomopenday W/dalvikvm: VFY: unable to resolve virtual method 254: Landroid/app/Notification$Builder;.setLocalOnly (Z)Landroid/app/Notification$Builder; 
01-16 10:49:15.298 3014-3014/com.example.dell.uomopenday D/dalvikvm: VFY: replacing opcode 0x6e at 0x00c8 
01-16 10:49:15.308 3014-3014/com.example.dell.uomopenday I/dalvikvm: Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzj 
01-16 10:49:15.308 3014-3014/com.example.dell.uomopenday W/dalvikvm: VFY: unable to resolve virtual method 549: Landroid/content/pm/PackageManager;.getPackageInstaller()Landroid/content/pm/PackageInstaller; 
01-16 10:49:15.308 3014-3014/com.example.dell.uomopenday D/dalvikvm: VFY: replacing opcode 0x6e at 0x000b 
01-16 10:49:15.358 3014-3037/com.example.dell.uomopenday I/GMPM: App measurement is starting up 
01-16 10:49:15.358 3014-3037/com.example.dell.uomopenday E/GMPM: getGoogleAppId failed with status: 10 
01-16 10:49:15.368 3014-3037/com.example.dell.uomopenday E/GMPM: Uploading is not possible. App measurement disabled 
01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;) 
01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested 
01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday W/dalvikvm: VFY: unable to resolve interface method 19469: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z 
01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode 
01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday W/dalvikvm: VFY: unable to resolve interface method 19473: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 
01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
01-16 10:49:15.408 3014-3014/com.example.dell.uomopenday I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations 
01-16 10:49:15.408 3014-3014/com.example.dell.uomopenday W/dalvikvm: VFY: unable to resolve virtual method 627: Landroid/content/res/TypedArray;.getChangingConfigurations()I 
01-16 10:49:15.408 3014-3014/com.example.dell.uomopenday D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
01-16 10:49:15.408 3014-3014/com.example.dell.uomopenday I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType 
01-16 10:49:15.408 3014-3014/com.example.dell.uomopenday W/dalvikvm: VFY: unable to resolve virtual method 649: Landroid/content/res/TypedArray;.getType (I)I 
01-16 10:49:15.408 3014-3014/com.example.dell.uomopenday D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
01-16 10:49:15.508 3014-3014/com.example.dell.uomopenday I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: () 
                    OpenGL ES Shader Compiler Version: E031.24.00.08 
                    Build Date: 03/21/14 Fri 
                    Local Branch: AU200+patches_03212014 
                    Remote Branch: 
                    Local Patches: 
                      Reconstruct Branch: 
01-16 10:49:15.758 3014-3014/com.example.dell.uomopenday D/OpenGLRenderer: Enabling debug mode 0 

buttononclick

button.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 

    Search = new ArrayList<>(); 

    int points = Integer.parseInt(edittext.getText().toString()); 
    for (int i = 0; i < courses.length; i++) { 

     Course c2 = courses[i]; 

     if (c2.getPoints() <= points) { 
      Search.add(c2); 
     } 

    } 

    Course searching [] = new Course[Search.size()]; 
    searching = Search.toArray(searching); 

    adapter = new listAdapter(MainActivity.this, searching); 
    myList.setAdapter(adapter); 

} 

String text = edittext.getText().toString(); 
if (text.matches("")) { 
button.setEnabled(false); 
return; 
    } 
}); 
+0

你有沒有試過'android:inputType =「number」' – Mohit

+0

我編輯了我的上面的問題,但仍然是我的應用程序崩潰。任何解決方案 –

+0

你可以發佈logcat – Mohit

回答

0

第一步,限制輸入類型爲數字。

在佈局:

android:inputType="number" 

或者代碼:

<EditText>.setInputType(InputType.TYPE_CLASS_NUMBER); 

然後驗證該文本是一個數字:

final String value = <EditText>.getText(); 
if (!StringUtils.isNullOrWhiteSpace(value) && value.matches("\\d*")) { 
    // The value is a number! Yay! 
} else { 
    // The value is NOT a number! Boo! 
} 

編輯:爲了完整這裏我的StringUtils(不需要):

public class StringUtils { 
    public static final String EMPTY = ""; 

    public static boolean isNullOrWhiteSpace(String value) { 
     if (value == null) { 
      return true; 
     } 

     for (int i = 0; i < value.length(); i++) { 
      if (!Character.isWhitespace(value.charAt(i))) { 
       return false; 
      } 
     } 

     return true; 
    } 
} 
-1

如果你只是想檢查您的EditText是空的或不是,你Button功能,你可以做到這一點像如下:

Button myButton 
EditText edittext = (EditText) findViewById(R.id.edittext); 

String text = edittext.getText().toString(); 
if (text.matches("")) { 
    myButton.setEnabled(false); 
    return; 
} 

通過我不明白是什麼做的方式你的意思是數字上是空的?但是,如果您只想輸入數字數據EditText,則可以在XML中的EditText中添加以下給定功能。

android:inputType="number" 
+0

我編輯了我的上面的問題,但仍然是我的應用程序崩潰。任何解決方案 –

+0

@MaryJane你得到了什麼錯誤,你可以通過編輯你的問題發佈複製並粘貼你的logcat錯誤,或者你可以在評論部分告訴我這裏。 –

+0

我沒有logcat錯誤。我的應用程序正在啓動更正我的應用程序,但點擊按鈕時,它只是崩潰。 –

相關問題