如果文本为空,则在多个编辑文本框上设置文本

时间:2017-10-14 12:36:00

标签: java android android-edittext

我在我的活动中创建了一个editText框,它就像一个更新表单,我现在要做的就是将随机值设置为所有复选框,无论哪个留空,提交按钮。我得到的是......仅在ETname edittext框中获取价值,如果我再次点击提交button,我的应用就会崩溃,即使我自己给予任何价值到ETname并提交它我的应用程序正在崩溃。请帮忙。

public class User_Profile extends AppCompatActivity实现了View.OnClickListener {

private Button Update;
private Context aContext;
private EditText ETname, ETsurname, ETadd, ETpin, ETmail, ETph;

ProgressDialog progressDialog;

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

    ETname = (EditText)findViewById(R.id.edit_name);
    ETsurname = (EditText)findViewById(R.id.edit_sur);
    ETadd = (EditText)findViewById(R.id.edit_add);
    ETpin = (EditText)findViewById(R.id.edit_pn);
    ETmail = (EditText)findViewById(R.id.edit_mail);
    ETph = (EditText)findViewById(R.id.edit_ph);

    Update = (Button)findViewById(R.id.update_btn);
    progressDialog = new ProgressDialog(this);

    Update.setOnClickListener(this);

}



@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.update_btn:
            proUpdate();
            break;
    }

}

private void proUpdate(){
    if (ETname.getText().toString().trim().equals("")){
        ETname.setText(SharedPrefManager.getInstance(this).getName());
    }
    if (ETsurname.getText().toString().trim().equals("")){
        ETsurname.setText(SharedPrefManager.getInstance(this).getSurname());
    }
    if (ETadd.getText().toString().trim().equals("")){
        ETadd.setText(SharedPrefManager.getInstance(this).getAddress());
    }
    if (ETpin.getText().toString().trim().equals("")){
        String pin = String.valueOf(SharedPrefManager.getInstance(this).getpin());
        ETpin.setText(pin);
    }
    if (ETmail.getText().toString().trim().equals("")){
        ETmail.setText(SharedPrefManager.getInstance(this).getUserEmail());
    }
    if (ETph.getText().toString().equals("")){
        ETph.setText(SharedPrefManager.getInstance(this).getUserPhone());
    }

        String reg_name = ETname.getText().toString().trim();
        String reg_surname = ETsurname.getText().toString().trim();
        String reg_address = ETadd.getText().toString().trim();
        String reg_pin = ETpin.getText().toString().trim();
        String reg_mail = ETmail.getText().toString().trim();
        String reg_phone = ETph.getText().toString().trim();


        String old_mail = (SharedPrefManager.getInstance(this).getUserEmail());
        int reg_id = (SharedPrefManager.getInstance(this).getid());

}

}

我的SharedPrefManager

公共类SharedPrefManager {

private static SharedPrefManager mInstance;
private static Context mCtx;
private static final String SHARED_PREF_NAME = "mysharedpref12";
private static final String KEY_USERNAME = "username";
private static final String KEY_USER_MAIL = "usermail";
private static final String KEY_USER_ID = "userid";
private static final String KEY_PHONE = "userphone";
private static final String KEY_NAME = "usename";
private static final String KEY_PIN = "pin";
private static final String KEY_SUR = "surname";
private static final String KEY_ADD = "address";

private SharedPrefManager(Context context) {
    mCtx = context;
}

public static synchronized SharedPrefManager getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new SharedPrefManager(context);
    }
    return mInstance;
}

public boolean userLogin(int id, int pin, String phone, String username, String mail, String name, String surname, String address/**, String catagory*/){

    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();

    editor.putInt(KEY_USER_ID, id);
    editor.putInt(KEY_PIN, pin);
    editor.putString(KEY_PHONE, phone);
    editor.putString(KEY_USERNAME, username);
    editor.putString(KEY_USER_MAIL, mail);
    editor.putString(KEY_NAME, name);
    editor.putString(KEY_SUR, surname);
    editor.putString(KEY_ADD, address);
    //editor.putString(KEY_CATA, catagory);

    editor.apply();

    return true;

}

public boolean isLoggedIn(){
    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    if (sharedPreferences.getString(KEY_USERNAME, null) != null){
        return true;
    }
        return false;
}

public boolean logOut(){
    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.clear();
    editor.apply();
    return true;
}

public int getid(){
    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    return sharedPreferences.getInt(KEY_USER_ID, Integer.parseInt(null));
}

public int getpin(){
    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    return sharedPreferences.getInt(KEY_PIN, Integer.parseInt(null));
}

public String getUsername(){
    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    return sharedPreferences.getString(KEY_USERNAME, null);
}

public String getUserEmail(){
    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    return sharedPreferences.getString(KEY_USER_MAIL, null);
}

public String getUserPhone(){
    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    return sharedPreferences.getString(KEY_PHONE, null);
}

public String getName() {
    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    return sharedPreferences.getString(KEY_NAME, null);
}

public String getSurname() {
    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    return sharedPreferences.getString(KEY_SUR, null);
}

public String getAddress() {
    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    return sharedPreferences.getString(KEY_ADD, null);
}

 /**public String getcatagory() {
    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    return sharedPreferences.getString(KEY_CATA, null);
}*/

}

2 个答案:

答案 0 :(得分:3)

以下是我告诉你的代码示例。

      private void proUpdate(){
       ...
        else if (ETpin.getText().toString().equals("")){
           string pin = String.valueOf(SharedPrefManager.getInstance(this).getpin());
           ETpin.setText(pin);
        }
       ...
     }

一次检查并在所有edittext中放置文本:

private void proUpdate(){
    if (ETname.getText().toString().trim().equals("")){
        ETname.setText(SharedPrefManager.getInstance(this).getName());
    } 
    if (ETsurname.getText().toString().trim().equals("")){
        ETsurname.setText(SharedPrefManager.getInstance(this).getSurname());
    } 
    if (ETadd.getText().toString().trim().equals("")){
        ETadd.setText(SharedPrefManager.getInstance(this).getAddress());
    } 
    if (ETph.getText().toString().equals("")){
        ETph.setText(SharedPrefManager.getInstance(this).getUserPhone());
    }  
    if (ETmail.getText().toString().trim().equals("")){
        ETmail.setText(SharedPrefManager.getInstance(this).getUserEmail());
    } 
    if (ETpin.getText().toString().equals("")){
        string pin = String.valueOf(SharedPrefManager.getInstance(this).getpin());
           ETpin.setText(pin);
    } 
    updateMethodCall(); // your method call
 }

只有在填写完所有ediitex时才会调用此updateMethod,并且您无需再次单击“提交”按钮。

答案 1 :(得分:2)

更改以下方法。

   public int getid(){
    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    return sharedPreferences.getInt(KEY_USER_ID, 0);
}

public int getpin(){
    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    return sharedPreferences.getInt(KEY_PIN,0);
}