我确实检查了一些关于如何检查editext为空的答案,我申请但是它不起作用。可能是因为我通过c#json WCF将JSON数据发送到数据库 在这里我做了什么
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPut httpPut= new HttpPut("http://localhost:4806/Service1.svc/UpdateData/Result");
httpPut.setHeader("Accept", "application/json");
httpPut.setHeader("Content-type", "application/json");
JSONStringer getdat = new JSONStringer()
.object()
.key("pd")
.object()
.key("ID").value(txtid.getText().toString())
.key("Name").value(txtname.getText().toString())
.key("Project").value(txtproject.getText().toString())
.key("Result").value(txttotal.getText().toString())
.endObject()
.endObject();
StringEntity entity = new StringEntity(getdat.toString(),"UTF-8");
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
entity.setContentType("application/json");
httpPut.setEntity(entity);
if((txtid.length()==0)&&(txtname.length()==0)&&(txtproject.length()==0)&&(txttotal.length()==0)) // Modification done here
{
Toast t =Toast.makeText(this, "Please enter all fields", Toast.LENGTH_LONG);
t.show();
}
else
{
HttpResponse response = httpClient.execute(httpPut);
Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode());
Toast.makeText(this, response.getStatusLine().getStatusCode() + "\n", Toast.LENGTH_LONG).show() ;
}
我正在为字符串实现它,我也想对int字段使用空检查,但我是android的新手并且不知道如何做到这一点。 这是int字段
h=Integer.parseInt(txtviva.getText().toString());
s=Integer.parseInt(txtpres.getText().toString());
x=Integer.parseInt(txtconfi.getText().toString());
y=Integer.parseInt(txtsystem.getText().toString());
z= h+s+x+y;
txttotal.setText(Integer.toString(z));
感谢大家的帮助。