2014-03-30 71 views
0

我有兩個TextView,兩個EditText和兩個Buttons。當按鈕被點擊而沒有在文本框中輸入任何值時,我想設置DialogBoxToast。我有兩個刺,ss1。如果ss1或者ss1沒有被輸入到edittext中,我必須得到一個敬酒。我寫了一個敬酒的代碼,但它不能正常工作!
你能幫我嗎?驗證爲空編輯文本字段不起作用

這是我的代碼:

public class MainActivity extends Activity implements OnClickListener { 
TextView name1; 
TextView name2; 
Button click; 
Button samegender; 
EditText boyname; 
EditText girlname; 

ImageView imgview; 
AnimationDrawable frameanimation; 
Bitmap bmp; 

String bread; 
String cheese; 
char sauce; 
MediaPlayer song; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //fullscreen 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow(). setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN ,           WindowManager LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.activity_main); 

    name1 = (TextView) findViewById(R.id.tvname1); 

    name2 = (TextView) findViewById(R.id.tvname2); 
    click = (Button) findViewById(R.id.btclick); 
samegender=(Button) findViewById(R.id.btsamegender); 
samegender.setOnClickListener(this); 
    boyname = (EditText) findViewById(R.id.etboyname); 
    boyname.setInputType(InputType.TYPE_CLASS_TEXT); 

    girlname = (EditText) findViewById(R.id.etgirlname); 
    girlname.setInputType(InputType.TYPE_CLASS_TEXT); 
      imgview=(ImageView)findViewById(R.id.imageanim); 
    imgview.setBackgroundResource(R.drawable.myanim); 
    frameanimation=(AnimationDrawable) imgview.getBackground(); 
     frameanimation.start(); 

    click.setOnClickListener(this); 
     } 


@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.btclick: 

     DataInputStream dis = new DataInputStream(System.in); 
     int i, 
     j = 0; 
     int d = 0; 
     int totlen; 
     String flames; 
     int[] newarr = new int[20]; 
     int[] newarr1 = new int[20]; 

     System.out.println("enter name"); 

     String s = boyname.getText().toString(); 

     StringBuffer sb = new StringBuffer(s); 

     char namearr[] = s.toCharArray(); 

     System.out.println("enter name"); 
     String s1 = girlname.getText().toString(); 
       //code for toast// 
       //if s nd s1 is empty then execute this else executee the rest// 
     if((s==" ") && (s1==" ")) 
     { 
    Toast.makeText(getBaseContext(),"cnt b empty" ,Toast.LENGTH_SHORT). show();            
     } 
     else 
     { 

     StringBuffer sb1 = new StringBuffer(s1); 
     System.out.println("the string1=" + s); 
     System.out.println("the string2=" + s1); 

     char namearr1[] = s1.toCharArray(); 
     try { 

      for (i = 0; i < sb.length(); i++) { 

       for (j = 0, d = 0; j < sb1.length(); j++) { 

      if (sb.charAt(i) == sb1.charAt(j)) { 
      sb.deleteCharAt(i); 
     System.out.println("the buff=" + sb); 

     sb1.deleteCharAt(j); 
     System.out.println("the buff=" + sb1); 
      i = 0; 
      break; 

        } 
       } 

      } 

     } catch (Exception e) { 
      System.out.println(e); 
     } 
     sb.length(); 
     System.out.println("string length=" + sb.length()); 
     sb1.length(); 
     System.out.println("string length=" + sb1.length()); 
     int len = sb.length() + sb1.length(); 
     totlen = len - 1; 
     System.out.println("string length=" + totlen); 
     String str = "flames"; 
     StringBuffer sb2 = new StringBuffer(str); 
     int index = 0; 

     str.length(); 
     int length = str.length(); 
     System.out.println("the length of flames is=" + str.length()); 
     while (index < length) { 
      char letter = str.charAt(index); 
      System.out.println(letter); 
      index = index + 1; 

     } 

     System.out.println(sb2.length()); 

     int m = 0, 
     n = 0; 
     for (m = 0;;) { 

      if (n == totlen) { 
       sb2.deleteCharAt(m); 

       System.out.println(sb2 + " m:" + m + " n:" + n); 
       System.out.println(sb2); 

       n = 0; 
       m--; 

      } else { 
       n++; 
      } 

      if (m == sb2.length() - 1) { 
       m = 0; 
      } else { 
       m++; 
      } 

      if (sb2.length() == 1) { 
       break; 
      } 

     } 

     char res = sb2.charAt(0); 
     System.out.println("the final char is=" + res); 

     String bread = boyname.getText().toString(); 
     String cheese = girlname.getText().toString(); 

     char sauce = res; 

     Bundle basket = new Bundle(); 
     basket.putString("name1", bread); 
     basket.putString("name2", cheese); 
     basket.putChar("ans", sauce); 
     Intent a = new Intent(MainActivity.this, ResultActivity.class); 
     a.putExtras(basket); 
     startActivity(a); 
     } 
     break; 
+1

可能重複(http://stackoverflow.com/questions/14250791/comparing-string- [比較串來串不工作當字符串從SQLiteDatabase來到] to-string-not-working-when-the-string-came-from-sqlitedatabase) – codeMagic

回答

1

您的驗證改成這樣:

if((s.equals("")) && (s1.equals(""))) 
    { 
Toast.makeText(getBaseContext(),"cnt b empty" ,Toast.LENGTH_SHORT).show();            
    } 

或者,您可以像這樣:

if((s.length() == 0) && (s1.length() == 0)) 
    { 
Toast.makeText(getBaseContext(),"cnt b empty" ,Toast.LENGTH_SHORT). show();            
    } 

想你的代碼和驗證工作:

import java.io.DataInputStream; 

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.drawable.AnimationDrawable; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.text.InputType; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity implements OnClickListener { 
TextView name1; 
TextView name2; 
Button click; 
Button samegender; 
EditText boyname; 
EditText girlname; 

ImageView imgview; 
Bitmap bmp; 

String bread; 
String cheese; 
char sauce; 
MediaPlayer song; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // fullscreen 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    // getWindow(). setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN 
    // ,WindowManager LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.activity_main); 

    name1 = (TextView) findViewById(R.id.tvname1); 

    name2 = (TextView) findViewById(R.id.tvname2); 
    click = (Button) findViewById(R.id.btclick); 
    samegender = (Button) findViewById(R.id.btsamegender); 
    samegender.setOnClickListener(this); 
    boyname = (EditText) findViewById(R.id.etboyname); 
    boyname.setInputType(InputType.TYPE_CLASS_TEXT); 

    girlname = (EditText) findViewById(R.id.etgirlname); 
    girlname.setInputType(InputType.TYPE_CLASS_TEXT); 
    imgview = (ImageView) findViewById(R.id.imageanim); 
    frameanimation = (AnimationDrawable) imgview.getBackground(); 
    frameanimation.start(); 

    click.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.btclick: 

     DataInputStream dis = new DataInputStream(System.in); 
     int i, 
     j = 0; 
     int d = 0; 
     int totlen; 
     String flames; 
     int[] newarr = new int[20]; 
     int[] newarr1 = new int[20]; 

     System.out.println("enter name"); 

     String s = boyname.getText().toString(); 

     StringBuffer sb = new StringBuffer(s); 

     char namearr[] = s.toCharArray(); 

     System.out.println("enter name"); 
     String s1 = girlname.getText().toString(); 
     // code for toast// 
     // if s nd s1 is empty then execute this else executee the rest// 
     if ((s.equals("")) || (s1.equals(""))) { 
      Toast.makeText(getBaseContext(), "cnt b empty", 
        Toast.LENGTH_SHORT).show(); 
     } else { 

      StringBuffer sb1 = new StringBuffer(s1); 
      System.out.println("the string1=" + s); 
      System.out.println("the string2=" + s1); 

      char namearr1[] = s1.toCharArray(); 
      try { 

       for (i = 0; i < sb.length(); i++) { 

        for (j = 0, d = 0; j < sb1.length(); j++) { 

         if (sb.charAt(i) == sb1.charAt(j)) { 
          sb.deleteCharAt(i); 
          System.out.println("the buff=" + sb); 

          sb1.deleteCharAt(j); 
          System.out.println("the buff=" + sb1); 
          i = 0; 
          break; 

         } 
        } 

       } 

      } catch (Exception e) { 
       System.out.println(e); 
      } 
      sb.length(); 
      System.out.println("string length=" + sb.length()); 
      sb1.length(); 
      System.out.println("string length=" + sb1.length()); 
      int len = sb.length() + sb1.length(); 
      totlen = len - 1; 
      System.out.println("string length=" + totlen); 
      String str = "flames"; 
      StringBuffer sb2 = new StringBuffer(str); 
      int index = 0; 

      str.length(); 
      int length = str.length(); 
      System.out.println("the length of flames is=" + str.length()); 
      while (index < length) { 
       char letter = str.charAt(index); 
       System.out.println(letter); 
       index = index + 1; 

      } 

      System.out.println(sb2.length()); 

      int m = 0, n = 0; 
      for (m = 0;;) { 

       if (n == totlen) { 
        sb2.deleteCharAt(m); 

        System.out.println(sb2 + " m:" + m + " n:" + n); 
        System.out.println(sb2); 

        n = 0; 
        m--; 

       } else { 
        n++; 
       } 

       if (m == sb2.length() - 1) { 
        m = 0; 
       } else { 
        m++; 
       } 

       if (sb2.length() == 1) { 
        break; 
       } 

      } 

      char res = sb2.charAt(0); 
      System.out.println("the final char is=" + res); 

      String bread = boyname.getText().toString(); 
      String cheese = girlname.getText().toString(); 

      char sauce = res; 

      Bundle basket = new Bundle(); 
      basket.putString("name1", bread); 
      basket.putString("name2", cheese); 
      basket.putChar("ans", sauce); 
      Intent a = new Intent(MainActivity.this, ResultActivity.class); 
      a.putExtras(basket); 
      startActivity(a); 
     } 
     break; 
    } 
} 
} 
+0

Iam沒有得到烤麪包!將其轉到下一個活動 – anusha

+0

,只需將&&條件更改爲|| –

+0

但我使用「等於」時出現錯誤。 Iam越來越像錯誤等於對象錯誤>當我改變對象Stil Eror持續存在「」這@Arshdeep – anusha

0

嘗試if (s.isEmpty() || s1.isEmpty())檢查您的字符串是否爲空。 ==.equals將不能與字符串比較一起使用,因爲它們比較對象而不僅僅是內容。要檢查兩個字符串是否相等,可以使用firstString.compareTo(anotherString) == 0

0

修剪那麼你的EditText值進行比較的

if((("").equals(s.trim())) && (("").equals(s1.trim()))) 
{ 
Toast.makeText(getBaseContext(),"cnt b empty" ,Toast.LENGTH_SHORT).show();            
}