我有这个登录活动,当用户点击登录或注册按钮时,会出现不同的EditTexts。我的问题是如何检查所有EditTexts是否已填写,以便启用continueButton?
(点击登录时,必须填写电子邮件和密码,点击注册时,必须填写电子邮件,密码和用户名)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/yellow"
android:orientation="vertical"
tools:context=".LoginActivity">
<TableRow
android:id="@+id/welcomeTableRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8pt">
<TextView
android:id="@+id/welcomeTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/welcome_to"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="normal|bold" />
</TableRow>
<TableRow
android:id="@+id/logoTableRow"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/welcomeLogoImageView"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_span="2"
android:contentDescription="@string/todo"
android:scaleType="fitCenter"
app:srcCompat="@drawable/logo_medium" />
</TableRow>
<TableRow
android:id="@+id/loginInfoTableRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12pt">
<TextView
android:id="@+id/infoEditText"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:padding="6pt"
android:text="@string/ask_to_register_or_login"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="18sp" />
</TableRow>
<TableRow
android:id="@+id/loginOrRegisterTableRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="4pt"
android:layout_marginEnd="40dp"
android:layout_marginStart="40dp"
android:layout_marginTop="12dp">
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:id="@+id/chooseToLoginButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_marginStart="10dp"
android:layout_weight="1"
android:background="@drawable/login_screen_button_unclicked"
android:onClick="chosenEvent"
android:text="@string/login" />
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:id="@+id/chooseToRegisterButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:background="@drawable/login_screen_button_unclicked"
android:onClick="chosenEvent"
android:text="@string/register" />
</TableRow>
<TableRow
android:id="@+id/nameTableRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="12dp"
android:layout_marginStart="12dp"
android:background="@android:color/white"
android:padding="4pt"
android:visibility="visible"
android:weightSum="1">
<TextView
android:id="@+id/nameTextView"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="0.3"
android:paddingEnd="2pt"
android:paddingStart="2pt"
android:text="@string/name"
android:textAlignment="textEnd"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold" />
<EditText
android:id="@+id/nameEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.65"
android:ems="10"
android:hint="@string/name"
android:inputType="textPersonName" />
</TableRow>
<TableRow
android:id="@+id/emailTableRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="12dp"
android:layout_marginStart="12dp"
android:background="@android:color/white"
android:padding="4pt"
android:visibility="visible"
android:weightSum="1">
<TextView
android:id="@+id/emailTextView"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="0.3"
android:paddingEnd="2pt"
android:paddingStart="2pt"
android:text="@string/email"
android:textAlignment="textEnd"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold" />
<EditText
android:id="@+id/emailEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.65"
android:ems="10"
android:hint="@string/email"
android:inputType="textEmailAddress" />
</TableRow>
<TableRow
android:id="@+id/passwordTableRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="12dp"
android:layout_marginStart="12dp"
android:background="@color/white"
android:padding="4pt"
android:visibility="visible"
android:weightSum="1">
<TextView
android:id="@+id/passwordTextView"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="0.3"
android:paddingEnd="2pt"
android:paddingStart="2pt"
android:text="@string/password"
android:textAlignment="textEnd"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold" />
<EditText
android:id="@+id/passwordEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.65"
android:ems="10"
android:hint="@string/password"
android:inputType="textPassword" />
</TableRow>
<TableRow
android:id="@+id/continueButtonTableRow"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="8pt"
android:visibility="visible">
<Button
android:id="@+id/continueButton"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="80dp"
android:layout_marginStart="80dp"
android:background="@drawable/login_screen_button_unclicked"
android:enabled="false"
android:text="@string/continue_button"
android:visibility="visible" />
</TableRow>
</TableLayout>
登录活动:
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import kotlinx.android.synthetic.main.activity_login.*
LoginActivity:
class LoginActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
}
private fun unclickAllChooseButtons() {
chooseToLoginButton.setBackgroundResource(R.drawable.login_screen_button_unclicked)
chooseToRegisterButton.setBackgroundResource(R.drawable.login_screen_button_unclicked)
}
private fun showEmailRow() {
emailTableRow.visibility = View.VISIBLE
}
private fun showNameRow() {
nameTableRow.visibility = View.VISIBLE
}
private fun showPasswordRow() {
passwordTableRow.visibility = View.VISIBLE
}
private fun hideAllRows() {
emailTableRow.visibility = View.GONE
passwordTableRow.visibility = View.GONE
nameTableRow.visibility = View.GONE
}
fun chosenEvent(view: View) {
when (view.id) {
R.id.chooseToLoginButton -> {
unclickAllChooseButtons()
chooseToLoginButton.setBackgroundResource(R.drawable.login_screen_button_clicked)
hideAllRows()
showEmailRow()
showPasswordRow()
}
R.id.chooseToRegisterButton -> {
unclickAllChooseButtons()
chooseToRegisterButton.setBackgroundResource(R.drawable.login_screen_button_clicked)
hideAllRows()
showNameRow()
showEmailRow()
showPasswordRow()
}
else -> {
unclickAllChooseButtons()
}
}
}
}
答案 0 :(得分:1)
这很难,因为你需要做的是实时启用按钮。所以你必须听每个EditText
,一旦条件符合书面文本,那么你应该把按钮设置为启用。这里还有另一个复杂性:如果用户返回然后尝试删除一些文本,会发生什么。解决这个问题的方法有两种,第一种是保存每个EditText的状态,第二种是验证要发送的数据。
//You have to set initial state as false representing conditione haven'met
//Otherwise when you try to get the tag is gonna be null
boolean initialValidity = false;
emailEt.setTag(initialValidity);
passEt.setTag(initialValidity);
emailEt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
String email = emailEt.getText().toString();
//You can improve this condition
if (email.trim().length() > 0 && email.contains("@") && email.contains(".")) {
emailEt.setTag(true);
if ((boolean) passEt.getTag()) {
sendButton.setEnabled(true);
}
} else {
emailEt.setTag(false);
sendButton.setEnabled(false);
}
}
});
passEt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
String password = passEt.getText().toString();
//You can improve this condition
if (!password.contains(" ") && password.length() > 6) {
passEt.setTag(true);
if ((boolean) emailEt.getTag()) {
sendButton.setEnabled(true);
}
} else {
passEt.setTag(false);
sendButton.setEnabled(false);
}
}
});
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email = emailEt.getText().toString();
String password = passEt.getText().toString();
//TODO add validation to send the data
//TODO send the data
}
});
我们在这里做的是听EditText
和用户写作时我们检查是否满足条件。 EditText
同时检查自己的状况以及其他条件。如果任何Button
违反条件中的条款,则此交叉检查允许我们启用或禁用EditText
。每个EditText
都可以通过检查标签来知道对方是否满足所需条件,该标签是表示是否满足条件的布尔值。我以enabled
为例,您可以使用setVisibility()
答案 1 :(得分:0)
这是您可以获得的所有帮助,因为您没有包含任何代码。另外,我的代码是用Java编写的,你应该明白这个想法。
if(!edittext1.equals("") && !edittext2.equals("")){
// enable button
}
答案 2 :(得分:0)
您必须使用不同的方法来验证表单是否已满,并完成这些验证:
public boolean validatePassword(String password){if(!password.equals("")){
if(password.length() == 6){ //Valid if the password is of certain sizes
this.password = password;
return true;
}
else {
Toast.makeText(this, "Length invalid", Toast.LENGTH_SHORT).show();
return false;
}
}
else {
Toast.makeText(this, "Please enter your password", Toast.LENGTH_SHORT).show();
return false;
}
}}
每种表格的方法。
您可以通过以下方式获取表单数据:
String pass = youreditText.getText()。toString();
最后:
if(validatePassword(password)){ if(validateOtherForm(text){
//What you want to do
}}