我无法理解为什么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 :(得分:1)
以下
错误etHost.toString().trim();
喜欢
etHost.getText().toString().trim();
答案 1 :(得分:1)
使用
host = etHost.getText().toString().trim();
username = etUsername.getText().toString().trim();
password = etPassword.getText().toString().trim();
调用getText()
从EditText获取值,而不是仅调用toString()
答案 2 :(得分:1)
host = etHost.toString().trim();
你在字符串中存储了EditText的文本描述..你想要的是它里面的实际文本..把它改成
host = etHost.getText().toString();