久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

空 EditText 的問題

Issue with empty EditText(空 EditText 的問題)
本文介紹了空 EditText 的問題的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有一個帶有 EditText、Button 和 TextView 的 Activity.如果我寫一些數字并單擊按鈕,TextView 會顯示計算結果.

I have an Activity with an EditText, Button and TextView. If I write some numbers and click the button, the TextView shows the result of the calculation.

如果 EditText 為空(即沒有輸入數字)并且我單擊按鈕,我會看到一個對話框:應用程序已意外停止.請重試".

If the EditText is empty (i.e. there are no numbers entered) and I click the Button I have the a dialog saying: "The application has stopped unexpectedly. Please try again".

請幫助我.如果用戶不寫任何數字,我想顯示一個帶有一些注釋的窗口(例如插入數字").我應該如何對 EditText 字段進行編程?如何解決這個問題?非常感謝

Please help me. I want to show a window with some comments (for example "Insert numbers"), if the user does not write any numbers. How should I program the EditText field? How to solve this? Thanks a lot

這是我的 onClick 方法:

This is my onClick method:

public void onClick(View v) {
    EditText numA  = (EditText)findViewById(R.id.editText1);
    TextView wynik1 = (TextView)findViewById(R.id.wynik);

    float num1 = Float.parseFloat(numA.getText().toString());

    float eq1 = 0;

    if(num1>0){ 
        switch (v.getId()) {
                case R.id.oblicz:
                    eq1 = 2*num1;
                break;
        }

        wynik1.setText(String.format("%f", eq1));   
    }
    else {
        Intent o = new Intent(this, Obliczokno.class);
        startActivity(o);
    }
}

我改變了onClick方法:

I have changed onClick method:

public void onClick(View v) {
    EditText numA  = (EditText)findViewById(R.id.editText1);

    float num2 = Float.parseFloat(numA.getText().toString());
    float eq1 = 0;
    float eq2 = 0;  

    try{  float num1 = Float.parseFloat(numA.getText().toString());

            if(num1>0 || num2>0){   

                switch (v.getId()) {
                case R.id.pole:
                    eq1 = 2*num1 ;

                break;  

                case R.id.obwod:
                    eq2 = 3*num1 ;

                break;  

                }}

                else {Intent o = new Intent(this, Obliczokno.class);
                startActivity(o);}
        }
         catch (NumberFormatException e) {
           Toast.makeText(this, "Sorry you did't type anything", Toast.LENGTH_SHORT).show();

           return;
        }

我有錯誤.我的 LogCat 錯誤:

and I have errors. My LogCat errors:

E/AndroidRuntime(321): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(321): java.lang.NumberFormatException:
E/AndroidRuntime(321):  at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:296)
E/AndroidRuntime(321):  at java.lang.Float.parseFloat(Float.java:327)

E/AndroidRuntime(321):  at arek.geometria.Oblicz.onClick(Oblicz.java:35)

E/AndroidRuntime(321):  at android.view.View.performClick(View.java:2344)

E/AndroidRuntime(321):  at android.view.View.onTouchEvent(View.java:4133)

E/AndroidRuntime(321):  at android.widget.TextView.onTouchEvent(TextView.java:6510)

E/AndroidRuntime(321):  at android.view.View.dispatchTouchEvent(View.java:3672)

E/AndroidRuntime(321):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)

E/AndroidRuntime(321):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)

E/AndroidRuntime(321):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)

E/AndroidRuntime(321):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)

E/AndroidRuntime(321):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)

E/AndroidRuntime(321):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)

E/AndroidRuntime(321):  at android.app.Activity.dispatchTouchEvent(Activity.java:1987)

E/AndroidRuntime(321):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)

E/AndroidRuntime(321):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)

E/AndroidRuntime(321):  at android.os.Handler.dispatchMessage(Handler.java:99)

E/AndroidRuntime(321):  at android.os.Looper.loop(Looper.java:123)

E/AndroidRuntime(321):  at android.app.ActivityThread.main(ActivityThread.java:4203)

E/AndroidRuntime(321):  at java.lang.reflect.Method.invokeNative(Native Method)

E/AndroidRuntime(321):  at java.lang.reflect.Method.invoke(Method.java:521)

E/AndroidRuntime(321):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)

E/AndroidRuntime(321):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)

E/AndroidRuntime(321):  at dalvik.system.NativeStart.main(Native Method)

if ... else 可以正常工作.你有什么想法嗎?請幫幫我.

The if ... else works correctly. Have you got any ideas? Please help me.

推薦答案

您只需要檢查用戶輸入的內容:

You just need to do a check on what the user has typed:

String input = editText.getText().toString();

if(input == null || input.trim().equals("")){
      Toast.makeText(context, "Sorry you did't type anything"), Toast.LENGTH_SHORT).show();
}

編輯

如果你有一個浮點數,你必須在解析浮點數之前做檢查:

If you have a float, you have to do the check before you parse the float:

String input = editText.getText().toString();

if(input == null || input.trim().equals("")){
      // Toast message
      return;
}
float num1 = Float.parseFloat(input);

try{

   float num1 = Float.parseFloat(numA.getText().toString());

} catch (NumberFormatException e) {
   // Toast message - you cannot get a float from that input
   return;
}

這篇關于空 EditText 的問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cut, copy, paste in android(在android中剪切、復制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數的數字)
Changing where cursor starts in an expanded EditText(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 国产欧美精品一区二区 | 一区二区三区国产精品 | 爱爱免费视频 | 国产乱性 | 男人天堂社区 | 韩日精品在线观看 | 日韩欧美在线观看 | 久久av网| 福利网址 | 在线一区视频 | 日韩在线免费视频 | 激情a| 二区成人 | 欧美一区二区在线观看 | 午夜精品一区二区三区在线视频 | 1000部精品久久久久久久久 | 国产91视频一区二区 | 色狠狠桃花综合 | 国产高清视频在线观看 | 亚洲欧美中文日韩在线v日本 | 精品久久久久久久人人人人传媒 | 亚洲精品888 | 黄色片网此 | 国产一区欧美一区 | 国产一级免费视频 | 高清国产一区二区 | 国产欧美精品 | 精品一二三区 | 五月婷婷在线播放 | 精品国产三级 | 成人免费视频网站在线看 | 中文字幕在线观看第一页 | 亚洲综合网站 | 999免费视频 | 日日夜夜精品免费视频 | 亚洲区一区二 | 午夜影院普通用户体验区 | 蜜月aⅴ免费一区二区三区 99re在线视频 | 人人澡人人射 | 国产午夜精品一区二区三区在线观看 | av毛片在线免费观看 |