2017-05-15 17 views
0

こんにちは私は、ユーザーが書籍を注文できる場所にアプリを作っています。彼または彼女は、自分のファー2番目の名前などで注文を記入し、インテントを使用して注文ボタンアプリケーションをヒットすると、SMSに切り替えるので、テキストメッセージ経由で書籍を購入します。しかし、私は、ユーザーが偶然に忘れた場合すべてのフィールドを入力し、「XYZフィールドを入力してください」というメッセージでトーストをポップアップします。私は他の場合に使用しましたが、一部のフィールドが空のままになったときに何か他のことが起こりました。私のアプリケーションが閉じられ、前のアクティビティに戻る必要があるというアンドロイドメッセージが表示されます。エラーメッセージは表示されません。androidの必須フィールド

public void kreiranjeNarudzbine(View v) { 
    EditText editTextIme = (EditText) findViewById(R.id.ime); 
    String imeNarucioca = editTextIme.getText().toString(); 
    EditText editTextPrezime = (EditText)findViewById(R.id.prezime); 
    String prezimeNarucioca = editTextPrezime.getText().toString(); 
    EditText editTelefonNarucioca = (EditText)findViewById(R.id.telefon); 
    String telefonNarucioca = editTelefonNarucioca.getText().toString(); 
    EditText editAdresaNarucioca = (EditText)findViewById(R.id.adresa); 
    String adresaNarucioca = editAdresaNarucioca.getText().toString(); 
    EditText editGradNarucioca = (EditText)findViewById(R.id.grad); 
    String gradNarucioca = editGradNarucioca.getText().toString(); 
    EditText editKolicina = (EditText)findViewById(R.id.kolicina); 
    String narucenaKolicina = editKolicina.getText().toString(); 
    int kolicina = Integer.parseInt(narucenaKolicina); 
    int cenaNarudzbine = cena(kolicina); 
    String poruka = sumiranjeNarudzbine(imeNarucioca, prezimeNarucioca,telefonNarucioca,adresaNarucioca,gradNarucioca,cenaNarudzbine); 
    Intent smsIntent = new Intent(Intent.ACTION_VIEW); 
    smsIntent.setType("vnd.android-dir/mms-sms"); 
    smsIntent.putExtra("address", "+381629647169"); 
    smsIntent.putExtra("sms_body",poruka); 
    if(imeNarucioca.equals("")){ 
     Toast.makeText(Narudzbina.this, "Unesite ime", Toast.LENGTH_LONG).show(); 
    } 
    else if(prezimeNarucioca.equals("")){ 
     Toast.makeText(Narudzbina.this,"Unesite Prezime", Toast.LENGTH_LONG).show(); 

    } 
    else if(telefonNarucioca.equals("")){ 
     Toast.makeText(Narudzbina.this,"Unesite kontakt telefon", Toast.LENGTH_LONG).show(); 
    } 
    else if(adresaNarucioca.equals("")){ 
     Toast.makeText(Narudzbina.this,"Unesite adresu",Toast.LENGTH_LONG).show(); 
    } 
    else if(gradNarucioca.equals("")){ 
     Toast.makeText(Narudzbina.this, "Navedite grad", Toast.LENGTH_LONG).show(); 
    } 
    else if(narucenaKolicina.equals("")){ 
     Toast.makeText(Narudzbina.this, "Navedite zeljenu kolicinu", Toast.LENGTH_LONG).show(); 
    } 
    else{ 

     startActivity(smsIntent);} 

} 
+0

から必要な色を使用し、代わりにyourTextフィールドに必要なフィールドを追加[MCVE]を投稿してください。 Android Studioで新しいプロジェクトを開始し、ただ1つのEditTextのコードを書くことをお勧めします。 –

+0

EditText.setError()も参照してください。 –

答えて

1

既に述べたように、ヌルでないか、空でないことを確認します。

if(imeNarucioca!=null && imeNarucioca.equals("")){ 
    Toast.makeText(Narudzbina.this, "Unesite ime", Toast.LENGTH_LONG).show(); 
} 
else if(prezimeNarucioca!=null && prezimeNarucioca.equals("")){ 
    Toast.makeText(Narudzbina.this,"Unesite Prezime", Toast.LENGTH_LONG).show(); 

} 
else if(telefonNarucioca!=null && telefonNarucioca.equals("")){ 
    Toast.makeText(Narudzbina.this,"Unesite kontakt telefon", Toast.LENGTH_LONG).show(); 
} 
else if(adresaNarucioca!=null && adresaNarucioca.equals("")){ 
    Toast.makeText(Narudzbina.this,"Unesite adresu",Toast.LENGTH_LONG).show(); 
} 
else if(gradNarucioca!=null && gradNarucioca.equals("")){ 
    Toast.makeText(Narudzbina.this, "Navedite grad", Toast.LENGTH_LONG).show(); 
} 
else if(narucenaKolicina!=null && narucenaKolicina.equals("")){ 
    Toast.makeText(Narudzbina.this, "Navedite zeljenu kolicinu", Toast.LENGTH_LONG).show(); 
} 
else{ 
    startActivity(smsIntent); 
} 

これは、できるだけ多くのテキストを防ぐための方法に転換することができます。

public boolean isETEmpty(EditText et){ 
    return (et != null && (et.equals("") || et.equals(" ")));//the final piece checks if the edittext is empty or just contains a space. It is or between 
    //in order to ensure that one of those are true. Thus the parenthesis 
} 

how the above works)今

、必要なフィールドにテキストをフォーマットするためにHTMLを使用することができます。

/*your view*/.setText(Html.fromHtml("Your title. <font color='red'>*</font>")); 

上記のコードでは、必須フィールドの後に赤い星印を追加してコードをフォーマットしています。

https://stackoverflow.com/a/9161958/6296561

+0

私にとってこれはいつも真実です –

0

空のEditTextの文字列がnullであるかもしれない、問題が発生します。

この

は私のコードです。次のコードを試してください。

if(imeNarucioca==null || imeNarucioca.equals("")){ 
     Toast.makeText(Narudzbina.this, "Unesite ime", Toast.LENGTH_LONG).show(); 
    } 
    else if(prezimeNarucioca==null || prezimeNarucioca.equals("")){ 
     Toast.makeText(Narudzbina.this,"Unesite Prezime", Toast.LENGTH_LONG).show(); 

    } 
    else if(telefonNarucioca==null || telefonNarucioca.equals("")){ 
     Toast.makeText(Narudzbina.this,"Unesite kontakt telefon", Toast.LENGTH_LONG).show(); 
    } 
    else if(adresaNarucioca==null || adresaNarucioca.equals("")){ 
     Toast.makeText(Narudzbina.this,"Unesite adresu",Toast.LENGTH_LONG).show(); 
    } 
    else if(gradNarucioca==null || gradNarucioca.equals("")){ 
     Toast.makeText(Narudzbina.this, "Navedite grad", Toast.LENGTH_LONG).show(); 
    } 
    else if(narucenaKolicina==null || narucenaKolicina.equals("")){ 
     Toast.makeText(Narudzbina.this, "Navedite zeljenu kolicinu", Toast.LENGTH_LONG).show(); 
    } 
    else{ 
     startActivity(smsIntent);} 
    } 
+0

コンテンツがヌルの場合は、 'et.equals(null)'をチェックする必要があります。 edittextの文字列が空でないかどうかを確認しています。 – Zoe

0
:あなたはTextViewの参照、のEditTextまたは任意のテキスト


さらに読書を設定するために使用して置き換えるコメント/*your view*/

/*your view*/.setText(Html.fromHtml("<font color='red'>*</font> Required field")); 

:その後ダウンあなたはこれを行うことができます

android.text.TextUtilsで提供されているisEmptyメソッドを使用する必要があります。全体の説明と、このメソッドの実装:

/** 
* Returns true if the string is null or 0-length. 
* @param str the string to be examined 
* @return true if str is null or zero length 
*/ 
public static boolean isEmpty(@Nullable CharSequence str) { 
    if (str == null || str.length() == 0) 
     return true; 
    else 
     return false; 
} 

をので、nullまたはemptyユーザー入力を検証するために、あなたはこのような何か行うことができます。

if(android.text.TextUtils.isEmpty(imeNarucioca)){ 
    // ... 
} 
0

ので、私はこれを使用していますが、あなたが行うことができますあなたが好きなもの、それと:

これはメソッドです:

public static boolean checkEditTextIsEmpty(EditText... editTexts) 
{ 
    try 
    { 
     for (EditText editText : editTexts) 
     { 
      if (editText.getText().toString().trim().length() == 0) 
      { 
       Drawable d = _application.currentActivity.getResources().getDrawable(android.R.drawable.ic_dialog_alert); 
       d.setBounds(0, 0, d.getIntrinsicWidth()/2, d.getIntrinsicHeight()/2); 
       editText.requestFocus(); 
       editText.setError(editText.getHint() + " is empty", d); 
       return false; 
      } 
     } 
    } 
    catch (Exception ignored) 
    { 
     return false; 
    } 
    return true; 
} 

これはプレビューです:(私のスタイルと背景を無視する) enter image description here

これは私がそれを実装する方法です:

if(checkEditTextIsEmpty(txtEmail, txtPassword)) 
{ 
    //Do whatever if EditTexts is not empty 
} 
0

ちょうどあなたのJavaファイルプロ文法的にこの行を追加します。 、 テキストビューの近くに必須フィールドを追加する。

tv.setText (Html.fromHtml(YourText 
       + " <font color='" 
       + getResources().getColor(R.color.colorAccent) + "'>" + " * " 
       + "</font>")); 

が、その後colors.xml

関連する問題