2013-10-15 47 views
1

我不明白爲什麼EditText給我一個奇怪的值,保存在我使用的變量中。EditText給出奇怪的字符串輸出

例如,當我保存這些EditText上一些變量的內容,這個值被保存:

variable: android.widget.EditText{41940958 VFED..CL .F...... 24,158-456,225 #7f080002 app:id/etHost} 

這是我的代碼:

public class MainActivity extends Activity { 

Button btnConnetti; 
EditText etUsername; 
EditText etPassword; 
EditText etHost; 

String host=null; 
String username=null; 
String password=null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    btnConnetti = (Button) findViewById(R.id.btnConnetti); 
    etUsername = (EditText) findViewById(R.id.etUsername); 
    etPassword = (EditText) findViewById(R.id.etPassword); 
    etHost = (EditText) findViewById(R.id.etHost); 

    btnConnetti.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      host = etHost.toString().trim(); 
      username = etUsername.toString().trim(); 
      password = etPassword.toString().trim(); 

      Log.d("deb","host: "+etHost.toString()); 

      if(host.isEmpty() || username.isEmpty() || password.isEmpty()) 
      { 
       new AlertDialog.Builder(getApplicationContext()) 
       .setTitle("Login fallito!") 
       .setMessage("Compilare tutti i campi richiesti.") 
       .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface arg0, int arg1) { 
         return; 
        } 
       }).create().show(); 
      } 
      else 
      { 
       myClickHandler(getCurrentFocus()); 

      } 
     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

public void myClickHandler(View view) { 
    ConnectivityManager connMgr = (ConnectivityManager) 
      getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 
    if (networkInfo != null && networkInfo.isConnected()) { 
     new InviaDati(host,username,password).execute(); 
    } else { 
     // display error 
    } 
} 


} 

class InviaDati extends AsyncTask<String, Integer, Void> 
{ 
InputStream is = null; 
String host,username,password; 

public InviaDati(String host,String username,String password) 
{ 
    this.host=host; 
    this.username=username; 
    this.password=password; 
} 

@Override 
protected void onPreExecute() 
{ 

} 

@Override 
protected Void doInBackground(String... params) { 
    // TODO Auto-generated method stub 

    Log.d("deb",host+"/callback.php?username="+username+"&password="+password); 

    try { 

     URL url = new URL(host+"/callback.php?username="+username+"&password="+password); 
     Log.d("deb",host+"/callback.php?username="+username+"&password="+password); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setReadTimeout(10000 /* milliseconds */); 
     conn.setConnectTimeout(15000 /* milliseconds */); 
     conn.setRequestMethod("GET"); 
     conn.setDoInput(true); 
     // Starts the query 
     conn.connect(); 
     int response = conn.getResponseCode(); 
     Log.d("DEBUG_TAG", "The response is: " + response); 
     is = conn.getInputStream(); 

     // Makes sure that the InputStream is closed after the app is 
     // finished using it. 
    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } finally { 
     if (is != null) { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 

    return null; 
} 

@Override 
protected void onPostExecute(Void result) 
{ 

} 
} 
+0

我想你一定要讀android文檔... – vgarzom

回答

1

錯在下面的

etHost.toString().trim(); 

確實像

etHost.getText().toString().trim(); 
+0

謝謝大家,一個愚蠢的錯誤:) –

1

使用

 host = etHost.getText().toString().trim(); 
     username = etUsername.getText().toString().trim(); 
     password = etPassword.getText().toString().trim(); 

呼叫getText()從EditText上獲得價值,而不是隻調用toString()

1
 host = etHost.toString().trim(); 

你存儲的字符串中的EditText的文字說明..你想要的是實際的文本里面呢..把它改成

 host = etHost.getText().toString();