我正在创建一个应用程序,我需要从用户那里获取输入并将该输入附加到字符串并将其用作数据解析的URL 但即使我在edittext中输入文本,编辑文本内容也为null 我将edittext内容转换为字符串,如下所示
EditText edit = (EditText)findViewById(R.Id.tv5);
在onclicklistener里面
String data = edit.getText().toString();
有人可以告诉我为什么data.length()
给我零吗?
我的完整主要活动如下:
public class Pnr extends Activity {
EditText edit;
TextView text1;
Button button;
String pnr, check;
HttpClient client;
JSONObject json;
int s;
String URL = "pnrbuddy.com/pnrstatus=";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pnr);
edit = (EditText) findViewById(R.id.tv5);
text1 = (TextView) findViewById(R.id.textView2);
button = (Button) findViewById(R.id.button1);
client = new DefaultHttpClient();
pnr = String.valueOf(edit.getText());
s = pnr.length();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Read().execute("trainNo");
}
});
}
public JSONObject pnrStatus(String key) throws ClientProtocolException,
IOException, JSONException {
StringBuilder url = new StringBuilder(URL);
url.append(key);
HttpGet get = new HttpGet(url.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
JSONObject last = new JSONObject();
if (status == 200) {
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
last = new JSONObject(data);
return last;
} else {
return last;
}
}
public class Read extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
json = pnrStatus(pnr);
return json.getString(arg0[0]);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return s + "";
}
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
text1.setText(" " + result);
}
}
} 当我输入字符串而不是接受来自字符串URL中的edittext以及代码工作的现有字符串 URL = “pnrbuddy / pnrstatus会将myText =”; 为什么我无法从editText中获取字符串?
答案 0 :(得分:0)
试试这个
EditText edit = (EditText)findViewById(R.id.tv5);
String data = edit.getText().toString();
答案 1 :(得分:0)
如果您尝试在onclicklistener
EditText
中获取长度,则仅向0
提供长度。在这种情况下,请尝试从onTextChange
获取。
答案 2 :(得分:0)
Replace your on create method by this n try
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pnr);
edit = (EditText) findViewById(R.id.tv5);
text1 = (TextView) findViewById(R.id.textView2);
button = (Button) findViewById(R.id.button1);
client = new DefaultHttpClient();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pnr =edit.getText().toString();
s = pnr.length();
new Read().execute("trainNo");
}
});
}
实际上,在创建活动时获取文本不是在单击按钮时获取文本,因此将gettext代码放在onclick方法中可以解决您的问题