2012-03-20 47 views
0

我正在创建一个用户名和密码为必填字段的登录页面。如果这些必填字段为空,则应该抛出错误消息“用户名为&密码为空”。但根据下面的代码没有任何反应。检查文本框为空或不是java android

以下是我的代码

public class Login_testingActivity extends Activity{ 
EditText edit1,edit2; 

/** Called when the activity is first created. */ 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    EditText edit1 = (EditText) findViewById(R.id.editText_username); 
    EditText edit2 = (EditText) findViewById(R.id.editText_password); 

    if (edit1.getText().length() != 0 && edit2.getText().length() != 0) { 
     fun(); 
    } else { 
     Toast.makeText(Login_testingActivity.this, "You need to enter a high AND low.", Toast.LENGTH_SHORT); 
    } 
} 
    public boolean fun(){ 

     Button next = (Button) findViewById(R.id.button_login); 
     next.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 
       Intent myIntent = new Intent(view.getContext(), Login_2.class); 
       startActivityForResult(myIntent, 0); 
      } 
     }); 

     return true; 
    } 
} 
+0

LogCat中的错误是什么? – 2012-03-20 05:57:12

+0

我的程序流程是否正确?请回复 – user1280083 2012-03-20 06:23:14

+0

否看到我的回答放在哪里if if conditon – 2012-03-20 06:29:27

回答

-1

使用此行作为

if (edit1.getText().toString().length() > 0 && edit2.getText().toString().length() > 0) { 
     fun(); 
    } else { 
     Toast.makeText(Login_testingActivity.this, "You need to enter a high AND low.", Toast.LENGTH_SHORT).show(); 
    } 
+0

这不起作用,因为调用Toast.makeText()时你没有调用show() – evilone 2012-03-20 06:01:09

+0

你是否正确检查了这个问题“检查文本框为空或不是java的android“不知道是什么问题不要给别人厌恶评论 – bindal 2012-03-20 06:05:10

+0

@bindal:你的else语句无论如何将无法正常工作。你甚至试过这个吗? – 2012-03-20 06:08:28

1

首先这条线if (edit1.getText().trim().length() != 0 && edit2.getText().trim().length() != 0) {

加入onClick(View view)和检查那里if(){startActivty(...);}else{Toast here..}

7
Toast.makeText(Login_testingActivity.this, "You need to enter a high AND low.", Toast.LENGTH_SHORT).show(); 
1

尝试这样,

if (edit1.getText().equalsIgnoreCase("") && edit2.getText().equalsIgnoreCase("")) 
    { 
     Toast.makeText(Login_testingActivity.this, "You need to enter a high AND low.", Toast.LENGTH_SHORT).show(); 
    } 
    else 
    { 
     fun(); 

    } 
+0

这是行不通的,因为调用Toast.makeText()时不调用show() – evilone 2012-03-20 06:01:35

+0

最后看到烤面包... – Hasmukh 2012-03-20 06:03:08

+0

你调用它是错误的 - show()是调用方法的正确方法。 .. – evilone 2012-03-20 06:03:35

0

我希望这对你的工作。

if (!edit1.getText().equals("") && !edit2.getText().equals("")) { 
    fun(); 
} else { 
    Toast.makeText(Login_testingActivity.this, "You need to enter a high AND low.", Toast.LENGTH_SHORT).show(); 
} 
+0

这是行不通的,因为调用Toast.makeText()时不调用show() – evilone 2012-03-20 06:00:49

+0

检查我的答案忘了附加.show();在吐司 – 2012-03-20 06:04:24