我有两个问题。
我有几个EditText
必须在点击按钮时进行验证。我用一切来检查edittexts是否为空,但它总是显示我在edittext中有一些东西并且不是空的。我后来删除了提示并且有效。
我尝试了edittext.getText().toString().trim().length()==0
,TextUtils.isEmpty(edittext.getText())
,edittext.matches("")
,但没有任何效果。
在不删除EditText
android:hint="blah"
是否为空
2.此外,我正在尝试为editName,editPhNo
设置错误。但它没有显示出来。它适用于editPassword,editConfirmPassword
。我的布局有问题吗?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.prematixsofs.taxiapp.EditUserDetails">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="fill_parent"
>
<!--Display UserDetails Layout-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/displayLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/custom_edittext"
android:gravity="center"
android:padding="8dp"
android:paddingLeft="10dp"
android:paddingRight="5dp" />
<TextView
android:id="@+id/phNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/custom_edittext"
android:gravity="center"
android:padding="8dp"
android:paddingLeft="10dp"
android:paddingRight="5dp" />
<TextView
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/custom_edittext"
android:gravity="center"
android:inputType="textEmailAddress"
android:padding="8dp"
android:paddingLeft="10dp"
android:paddingRight="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp">
<Button
android:id="@+id/edit"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="@drawable/button_custom"
android:text="Edit"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<!--Edit Layout -->
<LinearLayout
android:id="@+id/editLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<EditText
android:id="@+id/editName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/custom_edittext"
android:gravity="center"
android:hint="Enter Name"
android:padding="8dp"
android:paddingLeft="10dp"
android:paddingRight="5dp" />
<EditText
android:id="@+id/editPhNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/custom_edittext"
android:gravity="center"
android:hint="Enter new Phone Number"
android:padding="8dp"
android:paddingLeft="10dp"
android:paddingRight="5dp" />
<EditText
android:id="@+id/editPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/custom_edittext"
android:gravity="center"
android:hint="Enter new Password"
android:padding="8dp"
android:paddingLeft="10dp"
android:paddingRight="5dp"
android:password="true" />
<EditText
android:id="@+id/editConfirmPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/custom_edittext"
android:gravity="center"
android:hint="Confirm Password"
android:padding="8dp"
android:paddingLeft="10dp"
android:paddingRight="5dp"
android:password="true" />
<Button
android:id="@+id/saveOnEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="15dp"
android:layout_marginTop="60dp"
android:background="@drawable/button_custom"
android:gravity="center"
android:text="Save"
android:textColor="#ffffff" />
</LinearLayout>
saveOnEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(TextUtils.isEmpty(name.getText())&&TextUtils.isEmpty(phno.getText())&&TextUtils.isEmpty(editPassword.getText())&&TextUtils.isEmpty(editConfirmPassword.getText())) //Check if empty here
{
Toast.makeText(getApplicationContext(),"Enter the details you want to update",Toast.LENGTH_LONG).show();
}
else {
if (validate()) {
if (editConfirmPassword.getText().toString().equals(editPassword.getText().toString())) {
databaseHelper.saveOnEdit(sessionManager.getUserEmail(), editName.getText().toString(), editPhNo.getText().toString(), editPassword.getText().toString().toString());
editName.setText("");
editConfirmPassword.setText("");
editPassword.setText("");
editPhNo.setText("");
} else {
editConfirmPassword.setError("Passwords don't match");
editPassword.setError("Passwords don't match");
Toast.makeText(getApplicationContext(), "Passwords don't match", Toast.LENGTH_LONG).show();
}
} else {
phno.setError("Enter valid phone number");
Toast.makeText(getApplicationContext(), "Enter a valid Phone Number", Toast.LENGTH_LONG).show();
}
}
}
});
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_user_details);
edit = (Button) findViewById(R.id.edit);
name = (TextView) findViewById(R.id.name);
phno = (TextView) findViewById(R.id.phNo);
email = (TextView) findViewById(R.id.email);
editName = (EditText) findViewById(R.id.editName);
editPassword = (EditText) findViewById(R.id.editPassword);
editConfirmPassword = (EditText) findViewById(R.id.editConfirmPassword);
editPhNo = (EditText) findViewById(R.id.editPhNo);
userDetailList = new ArrayList();
displayLayout = (LinearLayout) findViewById(R.id.linearLayout);
editLayout = (LinearLayout) findViewById(R.id.editLayout);
editLayout.setVisibility(editLayout.GONE);
databaseHelper = new DatabaseHelper(getApplicationContext());
sessionManager = new SessionManager(getApplicationContext());
userDetailList = databaseHelper.displayUserDetails(sessionManager.getUserEmail());
name.setText(userDetailList.get(0).toString());
phno.setText(userDetailList.get(1).toString());
email.setText(userDetailList.get(2).toString());
displayLayout = (LinearLayout) findViewById(R.id.linearLayout);
editLayout = (LinearLayout) findViewById(R.id.editLayout);
saveOnEdit = (Button) findViewById(R.id.saveOnEdit);
答案 0 :(得分:0)
根据您对问题的修改,您正在检查错误的字段! name
和phno
是TextView
。将它们更改为实际EditText
,如下所示:
if(TextUtils.isEmpty(editName.getText())&&TextUtils.isEmpty(editPhNo.getText())&&TextUtils.isEmpty(editPassword.getText())&&TextUtils.isEmpty(editConfirmPassword.getText())) //Check if empty here
{
Toast.makeText(getApplicationContext(),"Enter the details you want to update",Toast.LENGTH_LONG).show();
}