将edittext值定义为字符串

时间:2014-04-26 14:26:28

标签: android

我有一个带返回值的字符串,我想将返回值与edittext的文本进行比较。 但我不能这样做。 我的代码是这样的:

    public static String  login (String a)
      {
            String Pass = null;
            InputStream is = null;
            String result = "";
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("UName", a));

            try{
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(LoginAddress);
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();

            }catch(Exception e){

                Log.e("log_tag", "Error in http connection "+e.toString());
                Pass = "Error Connection";
            }

            //convert response to string
            try{
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                            sb.append(line + "\n");
                    }
                    is.close();
                    result=sb.toString();
            }catch(Exception e){
                    Log.e("log_tag", "Error converting result "+e.toString());
                    Pass = "Convert Error";
            }
            //parse json data
            try{
                    JSONArray jArray = new JSONArray(result);
                    for(int i=0;i<jArray.length();i++){

                        JSONObject json_data = jArray.getJSONObject(i);
                            Log.i("log_tag", "UPWord: "+json_data.getString("UPWord"));
                            Pass = json_data.getString("UPWord");

                    }
            }catch(JSONException e){
                    Log.e("log_tag", "Error parsing data "+e.toString());
                    Pass = "Wrong Username or Password";
            }

            return Pass;
        }   

我称之为:

        public void onClick(View v)
        {
            StrictMode.setThreadPolicy(policy); 

            EditText Username = (EditText) findViewById(R.id.Usernme1);
            EditText Password = (EditText) findViewById(R.id.Passwordd1);


            String Result = DownLeftFragment.login(Username.getText().toString());

            if (Result == Password.getText().toString())
            {
                Intent intent = new Intent(MainActivity.this,HomePage.class);
                startActivity(intent);
            }
            else if (Result == "Wrong Username or Password")
            {
                Toast.makeText(MainActivity.this, "Wrong Username or Password", Toast.LENGTH_SHORT).show();     
            }
            else if (Result == "Error Connection")
            {
                Toast.makeText(MainActivity.this, "Error in network connection", Toast.LENGTH_SHORT).show();        
            }
            else if (Result == "Convert Error")
            {
                Toast.makeText(MainActivity.this, "Error in app", Toast.LENGTH_SHORT).show();       
            }
            else 
            {
                Toast.makeText(MainActivity.this, Result, Toast.LENGTH_SHORT).show();       

             }

        }

我定义了2个edittexts,Username1和passwordd1,它适用于Username1.getText()。toString(),但不适用于passwordd1。

(if)适用于所有(else if)s。但不是主if(if(Result == Password.getText()。toString()))

2 个答案:

答案 0 :(得分:2)

If (_Password.getText().toString().trim().equals("Wrong Username or Password"))
{ 

//your code

}

else 
{
 Toast.makeText(MainActivity.this, "Wrong Username or Password", Toast.LENGTH_SHORT).show();     

}

答案 1 :(得分:0)

正如@Bob Malooga建议使用equals进行字符串比较而不是==

只需替换

if (Result == Password.getText().toString())
        {
            Intent intent = new Intent(MainActivity.this,HomePage.class);
            startActivity(intent);
        }
else if (Result == "Wrong Username or Password")
        {
            Toast.makeText(MainActivity.this, "Wrong Username or Password", Toast.LENGTH_SHORT).show();     
        }
 else if (Result == "Error Connection")
        {
            Toast.makeText(MainActivity.this, "Error in network connection", Toast.LENGTH_SHORT).show();        
        }
 else if (Result == "Convert Error")
        {
            Toast.makeText(MainActivity.this, "Error in app", Toast.LENGTH_SHORT).show();       
        }

通过

if (Result.equals(Password.getText().toString()))
        {
            Intent intent = new Intent(MainActivity.this,HomePage.class);
            startActivity(intent);
        }
else if (Result.equals("Wrong Username or Password"))
        {
            Toast.makeText(MainActivity.this, "Wrong Username or Password", Toast.LENGTH_SHORT).show();     
        }
else if (Result.equals("Error Connection"))
        {
            Toast.makeText(MainActivity.this, "Error in network connection", Toast.LENGTH_SHORT).show();        
        }
else if (Result.equals("Convert Error"))
        {
            Toast.makeText(MainActivity.this, "Error in app", Toast.LENGTH_SHORT).show();       
        }