Android,使用链接到Firebase数据库的注册表单。如何强制使用所有Edittext字段?
使用一些编辑文本字段,在填写所有字段之前,提交按钮应该不可见/不起作用,并且也不应创建用户。
这是代码段
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_firebase);
//get data from previous activity
Intent intent=getIntent();
String RegAmount=intent.getStringExtra("regAmount");
String Ename=intent.getStringExtra("Ename");
//data recieved
inputName = (EditText) findViewById(R.id.name);
inputEmail = (EditText) findViewById(R.id.email);
inputUsn = (EditText) findViewById(R.id.usn);
inputEventName = (EditText) findViewById(R.id.EventName);
inputEventName.setText(Ename);
inputContact = (EditText) findViewById(R.id.phone);
inputCollege = (EditText) findViewById(R.id.college);
inputRegAmount = (EditText) findViewById(R.id.RegAmount);
inputRegAmount.setText(RegAmount);
SubmitForm = (Button) findViewById(R.id.RegButton);
mFirebaseInstance = FirebaseDatabase.getInstance();
mFirebaseDatabase = mFirebaseInstance.getReference("users");
mFirebaseInstance.getReference("app_title").setValue("Saviskara Registration Form");
// app_title change listener
mFirebaseInstance.getReference("app_title").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
Log.e(TAG, "App title updated");
String appTitle = dataSnapshot.getValue(String.class);
// update toolbar title
getSupportActionBar().setTitle(appTitle);
}
@Override
public void onCancelled(DatabaseError error)
{
// Failed to read value
Log.e(TAG, "Failed to read app title value.", error.toException());
}
});
// Save the user
SubmitForm.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
String name = inputName.getText().toString();
String email = inputEmail.getText().toString();
String usn=inputUsn.getText().toString();
String eventName=inputEventName.getText().toString();
String contact=inputContact.getText().toString();
String college =inputCollege.getText().toString();
String regamount=inputRegAmount.getText().toString();
createUser(name,email,usn,contact,regamount,eventName,college);
Toast.makeText(Firebase.this,"Thank You!",Toast.LENGTH_SHORT).show();
Intent i = new Intent(Firebase.this,MainActivity.class);
startActivity(i);
finish();
}
});
}
private void createUser(String name, String email,String usn,String pno,String regamount,String ename,String colName)
{
// TODO
userId = mFirebaseDatabase.push().getKey();
User user = new User(name,email,usn,pno,regamount,ename,colName);
mFirebaseDatabase.child(userId).setValue(user);
addUserChangeListener();
}
private void addUserChangeListener()
{
// User data change listener
mFirebaseDatabase.child(userId).addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
User user = dataSnapshot.getValue(User.class);
// Check for null
if (user == null)
{
Log.e(TAG, "User data is null!");
return;
}
// clear edit text
inputEmail.setText("");
inputName.setText("");
inputCollege.setText("");
inputContact.setText("");
inputRegAmount.setText("");
inputUsn.setText("");
inputEventName.setText("");
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.e(TAG, "Failed to read user", error.toException());
}
});
}
}
我可以通过 InstaMojo 添加付款网关,只有在付款成功后才能进入FireBase数据库吗?
也无法找到firebase的InstaMojo指南,只是java python和php。
这是否意味着我需要一个新的后端?
答案 0 :(得分:1)
您可以检查EditText
的验证:
SubmitForm.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
if(checkValidation()) {
String name = inputName.getText().toString().trim();
String email = inputEmail.getText().toString().trim();
String usn = inputUsn.getText().toString().trim();
String eventName = inputEventName.getText().toString().trim();
String contact = inputContact.getText().toString().trim();
String college = inputCollege.getText().toString().trim();
String regamount = inputRegAmount.getText().toString().trim();
createUser(name, email, usn, contact, regamount, eventName, college);
Toast.makeText(Firebase.this, "Thank You!", Toast.LENGTH_SHORT).show();
Intent i = new Intent(Firebase.this, MainActivity.class);
startActivity(i);
finish();
}
}
});
和checkValidation
方法将是:
public boolean checkValidation() {
String name = inputName.getText().toString().trim();
String email = inputEmail.getText().toString().trim();
String usn = inputUsn.getText().toString().trim();
String eventName = inputEventName.getText().toString().trim();
String contact = inputContact.getText().toString().trim();
String college = inputCollege.getText().toString().trim();
String regamount = inputRegAmount.getText().toString().trim();
if (name.length() <= 0) {
inputName.requestFocus();
inputName.setError("Enter Name");
return false;
} else if (email.length() <= 0) {
inputEmail.requestFocus();
inputEmail.setError("Enter Email");
return false;
} else if (usn.length() <= 0) {
inputUsn.requestFocus();
inputUsn.setError("Enter USN");
return false;
} else if (eventName.length() <= 0) {
inputEventName.requestFocus();
inputEventName.setError("Enter EventName");
return false;
} else if (contact.length() <= 0) {
inputContact.requestFocus();
inputContact.setError("Enter Contact");
return false;
} else if (college.length() <= 0) {
inputCollege.requestFocus();
inputCollege.setError("Enter Collage");
return false;
} else if (regamount.length() <= 0) {
inputRegAmount.requestFocus();
inputRegAmount.setError("Enter RegAmount");
return false;
} else {
return true;
}
}
对于提交按钮可见性,您可以将TextWatcher
与所有EditText
字段一起使用。
答案 1 :(得分:0)
对于您的第一个问题 - 在填写所有字段之前,按钮应该不可见/不起作用:
您需要实施TextWatcher以检查EditText(Example 1)中的输入更改,或使用ButterKnife(Example 2)使用OnTextChanged()
。
答案 2 :(得分:-1)
您可以在每个编辑文本下方创建一个视图,并且可以每隔1500毫秒在一个处理程序中检查编辑文本是否为空,如果符合条件则显示视图。
Handler handler = new Handler();
int delay = 1000; //milliseconds
handler.postDelayed(new Runnable(){
public void run(){
if(edittext1.isEmpty()){
view1.setvisibilty(View.VISIBLE);
}
else if(edittext2.isEmpty()){
view2.setvisibilty(View.VISIBLE);
}
and so on
handler.postDelayed(this, delay);
}
}, delay);