0

我需要你的帮助我不知道我应该在哪里写 if 条件来检查文本框是否为空的问题请检查我的代码:

public class register extends Activity {

private ProgressDialog pDialog;

JSONParser jsonParser = new JSONParser();
EditText inputName;
EditText inputUser;
EditText inputPass;
EditText inputAge;
EditText inputBloodType;

// url to create new product
private static String url_create_product = "http://10.0.2.2/blood_needed/create_product.php" ;

// JSON Node names
private static final String TAG_SUCCESS = "success";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);
    inputName = (EditText) findViewById(R.id.editText1);
    inputUser = (EditText) findViewById(R.id.editText5);
    inputPass = (EditText) findViewById(R.id.editText3);
    inputAge = (EditText) findViewById(R.id.editText2);
    inputBloodType = (EditText) findViewById(R.id.editText4);

    // Create button
    Button btnRegister = (Button) findViewById(R.id.Button01);

    // button click event
    btnRegister.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // creating new product in background thread
            new CreateNewProduct().execute();
        }
    });}

class CreateNewProduct extends AsyncTask<String, String, String> {

/**
 * Before starting background thread Show Progress Dialog
 * */
@Override
protected void onPreExecute() {
    super.onPreExecute();
    pDialog = new ProgressDialog(register.this);
    pDialog.setMessage("Regestering..");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(true);
    pDialog.show();
}



protected String doInBackground(String... args) {
    String name = inputName.getText().toString();
    String user = inputUser.getText().toString();
    String pass = inputPass.getText().toString();
    String age = inputAge.getText().toString();
    String bloodtype = inputBloodType.getText().toString();


    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("name", name));
    params.add(new BasicNameValuePair("username", user));
    params.add(new BasicNameValuePair("password", pass));
    params.add(new BasicNameValuePair("age", age));
    params.add(new BasicNameValuePair("bloodtype", bloodtype));

    // getting JSON Object
    // Note that create product url accepts POST method
    JSONObject json = jsonParser.makeHttpRequest(url_create_product,
            "POST", params);

    // check log cat fro response
    Log.d("Create Response", json.toString());

    // check for success tag
    try {
        int success = json.getInt(TAG_SUCCESS);

        if (success == 1) {
            // successfully created product
            // closing this screen
            finish();
        } else {
            // failed to create product
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return null;
}

/**
 * After completing background task Dismiss the progress dialog
 * **/
protected void onPostExecute(String file_url) {
    Toast.makeText(getBaseContext(), "Succes!", Toast.LENGTH_SHORT).show();

    // dismiss the dialog once done
    pDialog.dismiss();
}}
4

1 回答 1

1

放进去onClick()

btnRegister.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // creating new product in background thread
            String name = inputName.getText().toString();
    String user = inputUser.getText().toString();
    String pass = inputPass.getText().toString();
    String age = inputAge.getText().toString();
    String bloodtype = inputBloodType.getText().toString();
       if(user.equals("") || pass.equals("") || age.equals("") || bloodtype.equals("") || name.equals("")){
             // add message here
           }else{
            new CreateNewProduct().execute();
           }
        }
    });}
于 2013-04-06T19:14:26.833 回答