gpt4 book ai didi

android - 如何获取开关按钮的值并存储在数据库中?

转载 作者:行者123 更新时间:2023-11-30 00:48:52 25 4
gpt4 key购买 nike

我正在制作一个注册页面,其中订阅短信选项带有开关切换按钮。如果客户切换该按钮,则值(真或假)应保存在数据库中。我无法找到解决方案

这是我的 Registration.class:

public class RegistrationForm extends AppCompatActivity {

EditText fn,ln,mb,em,pw,cpw,dob,gen;
Switch sw;
RadioGroup male,feml;
Switch swth;
private ProgressDialog pDialog;
String status="";


public final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile(
"[a-zA-Z0-9+._%-+]{1,256}" +
"@" +
"[a-zA-Z0-9][a-zA-Z0-9-]{0,64}" +
"(" +
"." +
"[a-zA-Z0-9][a-zA-Z0-9-]{0,25}" +
")+"
);


private static String url_create_book = "http://cloud....com/broccoli/creatinfo.php";

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


String rval;

JSONParser jsonParser = new JSONParser();
private int serverResponseCode = 0;
Context c;
int i=0;


Button sub;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration_form);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);


fn=(EditText)findViewById(R.id.fnm) ;
ln=(EditText)findViewById(R.id.lnm) ;
mb=(EditText)findViewById(R.id.mobile) ;




pw=(EditText)findViewById(R.id.pass) ;
cpw=(EditText)findViewById(R.id.cpass) ;

dob=(EditText)findViewById(R.id.dob);

dob.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
int mYear, mMonth, mDay;
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(RegistrationForm.this,R.style.datepicker, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
dob.setText(year + "/" + (month + 1) + "/" + dayOfMonth);
// dob.setText(dayOfMonth + "/" + (month + 1) + "/" + );
}
}, mYear, mMonth, mDay);
//forsetting minimum date for selection
// datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
datePickerDialog.show();
}
});


// RadioButton male=(RadioButton)findViewById(R.id.rgm) ;

// RadioButton feml=(RadioButton)findViewById(R.id.rgf) ;

Switch swth=(Switch)findViewById(R.id.mySwitch) ;
String status="false";

//////set the switch to ON
swth.setChecked(false);

//////attach a listener to check for changes in state
swth.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

if(isChecked){
status="true"; //edit here
}else{
status="false";
}

}
});


RadioGroup rgrp=(RadioGroup)findViewById(R.id.rg);

RadioButton radioButton;





getSupportActionBar().setDisplayHomeAsUpEnabled(true);
sub=(Button)findViewById(R.id.sub2);




sub.setOnClickListener(new View.OnClickListener() {



@Override
public void onClick(View v) {
RadioGroup rgrp = (RadioGroup) findViewById(R.id.rg);
em = (EditText) findViewById(R.id.email);

RadioButton radioButton;

int selectedId = rgrp.getCheckedRadioButtonId();

// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);
rval = radioButton.getText().toString();
// Toast.makeText(RegistrationForm.this, rval, Toast.LENGTH_SHORT).show();


String email = em.getText().toString();


if(checkEmail(email))
Toast.makeText(RegistrationForm.this,"Valid Email Addresss", Toast.LENGTH_SHORT).show();
else
Toast.makeText(RegistrationForm.this,"Invalid Email Addresss", Toast.LENGTH_SHORT).show();
new CreateNewProduct().execute();



// startActivity(new Intent(RegistrationForm.this, Home.class));


}
private boolean checkEmail(String email) {
return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
}
});


}







class CreateNewProduct extends AsyncTask<String, String, String> {
private String fname;
private String lname;
private String email;
private String passwrd;
private String cpasswrd;
private String dobr;



/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(RegistrationForm.this);
pDialog.setMessage("Creating books..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
fname = fn.getText().toString();
lname = ln.getText().toString();
email = em.getText().toString();
passwrd = pw.getText().toString();
cpasswrd = cpw.getText().toString();
dobr = dob.getText().toString();

//Toast.makeText(RegistrationForm.this,
//dobr, Toast.LENGTH_SHORT).show();
}




protected String doInBackground(String... args) {


// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("First_Name", fname));
params.add(new BasicNameValuePair("Last_Name",lname));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("Gender", rval));
params.add(new BasicNameValuePair("password", passwrd));
params.add(new BasicNameValuePair("confirmPasw",cpasswrd));
params.add(new BasicNameValuePair("DOB",dobr));
params.add(new BasicNameValuePair("sms_subscrb",status));



// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_book,
"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
Intent i = new Intent(getApplicationContext(), Login.class);
startActivity(i);

// 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) {
// dismiss the dialog once done
pDialog.dismiss();
}

}



























@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

}

最佳答案

像那样处理你的开关按钮......

/////string that store switch state.. put this status while sending values to database
String status="false";

//////set the switch to ON
mySwitch.setChecked(false);

//////attach a listener to check for changes in state
mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

if(isChecked){
status="true"; //edit here
}else{
status="false"; //edit here
}

}
});

像这样发送到数据库..

params.add(new BasicNameValuePair("togglestatus",status));

关于android - 如何获取开关按钮的值并存储在数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41374946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com