我正在开发一个 Android 应用程序,其中登录活动有 editText、RadioButton、Spinner 和一个按钮。因此,当按下按钮时,我必须通过检查是否填写了所有字段来验证我的表单,否则会向谁能帮我写一下java代码吗?
提前致谢。
![A Screenshot of my Login Activity](https://i.stack.imgur.com/WDPiU.png)
public void OnClickListener(View v)
{
name=(EditText)findViewById(R.id.editText1);
years1=(RadioButton)findViewById(R.id.radioButton3);
years2=(RadioButton)findViewById(R.id.radioButton4);
years3=(RadioButton)findViewById(R.id.radioButton5);
manager=(RadioButton)findViewById(R.id.radioButton1);
teamleader=(RadioButton)findViewById(R.id.radioButton2);
rg1=(RadioGroup)findViewById(R.id.Designation);
rg2=(RadioGroup)findViewById(R.id.Years);
Button proceed = (Button) findViewById(R.id.button1);
proceed.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (validationSuccess())
{
}
else if(manager.isChecked())
{
Intent managerIntent = new Intent(getApplicationContext(), ManagerQuestionnaire1.class); // <----- START "SEARCH" ACTIVITY
startActivityForResult(managerIntent, 0);
}
else if(teamleader.isChecked())
{
Intent teamleaderIntent = new Intent(getApplicationContext(), TeamleaderQuestionnaire1.class); // <----- START "TYPE ENTRIES OUT" ACTIVITY
startActivityForResult(teamleaderIntent, 0);
}
else {
AlertDialog();
}
}
});
}
private Boolean validationSuccess()
{
if(name.getText().toString().equalsIgnoreCase(""))
{
Toast.makeText(getApplicationContext(),"Please enter name",0).show();
return false;
}
if(rg1.getCheckedRadioButtonId()<=0)
{
Toast.makeText(getApplicationContext(),"Please select designation",0).show();
return false;
}
if(rg2.getCheckedRadioButtonId()<=0)
{
Toast.makeText(getApplicationContext(),"Please select Experience",0).show();
return false;
}
if(dept.getSelectedItemPosition()==0)
{
Toast.makeText(getApplicationContext(),"Please select Depatrment",0).show();
return false;
}
return true;
}
private void AlertDialog()
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Login.this);
alertDialogBuilder.setMessage("Please ensure all Questions are answered").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
使用下面的代码:
mBtnNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (validationSuccess()) {
Intent mIntent = new Intent(
YourActivity.this,
NewActivity.class);
} else {
AlertDialog();
}
});
private Boolean validationSuccess(){
if(editUsername.getText().toString().equalsIgnoreCase("")){
Toast.makeText(getApplicationContext();"Please enter name",0).show();
return false;
}
if(radioGroupDesignation.getCheckedRadioButtonId()<=0){
Toast.makeText(getApplicationContext();"Please select designation",0).show();
return false;
}
if(radioGroupExp.getCheckedRadioButtonId()<=0){
Toast.makeText(getApplicationContext();"Please select Experience",0).show();
return false;
}
if(spinner.getSelectedItemPosition()==0){
Toast.makeText(getApplicationContext();"Please select Depatrment",0).show();
return false;
}
return true;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)