多个编辑文本验证字段是否包含在保存按钮中可见的数据

我已经阅读了很多帖子并提出了所有建议,但没有一个奏效。首先按钮的视图将其 setVisibility 保存到 xml 中的 GONE。

当我模拟应用程序时,保存按钮消失了,但是当我填写 editText 字段时,它应该出现按钮!但这不会发生!。


这是更新后的代码:


 public void GuardarAutomoviles() {


//        if(image!=null) {


        String objMarca = edMarca.getText().toString().trim();

        int objChasis = Integer.parseInt(edChasis.getText().toString().trim());

        String objColor = edColor.getText().toString().trim();

        int objKilometraje = Integer.parseInt(edKilometraje.getText().toString().trim());

        int objModelo = Integer.parseInt(edModelo.getText().toString().trim());

        String objPlaca = edPlaca.getText().toString().trim();

        String objMotor = edMotor.getText().toString().trim();

        int objPrecio = Integer.parseInt(edPrecio.getText().toString().trim());

        String objReferencia = edReferencia.getText().toString().trim();

        String objSucursal = edSucursal.getText().toString().trim();



        edMarca.addTextChangedListener(adminTextwatcher);

        edChasis.addTextChangedListener(adminTextwatcher);

        edColor.addTextChangedListener(adminTextwatcher);

        edKilometraje.addTextChangedListener(adminTextwatcher);

        edModelo.addTextChangedListener(adminTextwatcher);

        edPlaca.addTextChangedListener(adminTextwatcher);

        edMotor.addTextChangedListener(adminTextwatcher);

        edPrecio.addTextChangedListener(adminTextwatcher);

        edReferencia.addTextChangedListener(adminTextwatcher);

        edSucursal.addTextChangedListener(adminTextwatcher);




        if (!chasis.isEmpty()

                || !color.isEmpty()

                || !kilometraje.isEmpty()

                || !modelo.isEmpty()

                || !placa.isEmpty()

                || !motor.isEmpty()

                ||!precio.isEmpty()

                || !referencia.isEmpty()

                || !sucursal.isEmpty()) {

            admBotonGuardar.setVisibility(View.VISIBLE);

        }

largeQ
浏览 135回答 2
2回答

慕妹3146593

在这里解决这个问题的方法是我没有在 onCreate 中这样做。所以无论如何我会发布答案,这可以帮助很多人!代码:package com.example.mac.udacitycapstonefinalproject;import android.content.Intent;import android.net.Uri;import android.opengl.Visibility;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.text.Editable;import android.text.TextUtils;import android.text.TextWatcher;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.Toast;import com.example.mac.udacitycapstonefinalproject.Model.Automoviles;import com.google.android.gms.tasks.OnSuccessListener;import com.google.android.gms.tasks.Task;import com.google.firebase.auth.FirebaseAuth;import com.google.firebase.auth.FirebaseUser;import com.google.firebase.database.DatabaseReference;import com.google.firebase.database.FirebaseDatabase;import com.google.firebase.storage.FirebaseStorage;import com.google.firebase.storage.StorageReference;import com.google.firebase.storage.UploadTask;import java.util.ArrayList;import java.util.List;import butterknife.BindView;import butterknife.ButterKnife;public class AdminAddCar extends AppCompatActivity {&nbsp; &nbsp; @BindView(R.id.image_add_admin_layout)&nbsp; &nbsp; ImageView imageAddAdminLayout;&nbsp; &nbsp; @BindView(R.id.add_car_photo_admin)&nbsp; &nbsp; ImageButton addCarPhotoAdmin;&nbsp; &nbsp; @BindView(R.id.ed_marca)&nbsp; &nbsp; EditText edMarca;&nbsp; &nbsp; @BindView(R.id.ed_chasis)&nbsp; &nbsp; EditText edChasis;&nbsp; &nbsp; @BindView(R.id.ed_color)&nbsp; &nbsp; EditText edColor;&nbsp; &nbsp; @BindView(R.id.ed_kilometraje)&nbsp; &nbsp; EditText edKilometraje;&nbsp; &nbsp; @BindView(R.id.ed_modelo)&nbsp; &nbsp; EditText edModelo;&nbsp; &nbsp; @BindView(R.id.ed_motor)&nbsp; &nbsp; EditText edMotor;&nbsp; &nbsp; @BindView(R.id.ed_placa)&nbsp; &nbsp; EditText edPlaca;&nbsp; &nbsp; @BindView(R.id.ed_precio)&nbsp; &nbsp; EditText edPrecio;&nbsp; &nbsp; @BindView(R.id.ed_referencia)&nbsp; &nbsp; EditText edReferencia;&nbsp; &nbsp; @BindView(R.id.ed_sucursal)&nbsp; &nbsp; EditText edSucursal;&nbsp; &nbsp; String uId;&nbsp; &nbsp; String image;&nbsp; &nbsp; String marca;&nbsp; &nbsp; String chasis;&nbsp; &nbsp; String color;&nbsp; &nbsp; String kilometraje;&nbsp; &nbsp; String modelo;&nbsp; &nbsp; String placa;&nbsp; &nbsp; String motor;&nbsp; &nbsp; String precio;&nbsp; &nbsp; String referencia;&nbsp; &nbsp; String sucursal;&nbsp; &nbsp; FirebaseStorage firebaseStorage;&nbsp; &nbsp; StorageReference storageReference;&nbsp; &nbsp; DatabaseReference databaseReference;&nbsp; &nbsp; private static final int RC_PHOTO_PICKER = 2;&nbsp; &nbsp; @BindView(R.id.adm_boton_guardar)&nbsp; &nbsp; Button admBotonGuardar;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(@Nullable Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.admin_add_layout);&nbsp; &nbsp; &nbsp; &nbsp; ButterKnife.bind(this);&nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Implement the photo picker&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; databaseReference = FirebaseDatabase.getInstance().getReference("Automoviles");&nbsp; &nbsp; &nbsp; &nbsp; firebaseStorage = FirebaseStorage.getInstance();&nbsp; &nbsp; &nbsp; &nbsp; storageReference = firebaseStorage.getReference().child("carUploads");&nbsp; &nbsp; &nbsp; &nbsp; addCarPhotoAdmin.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO: Fire an intent to show an image picker&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(Intent.ACTION_GET_CONTENT);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.setType("image/jpeg");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; admBotonGuardar.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GuardarAutomoviles();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finish();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; TextWatcher adminTextwatcher = new TextWatcher() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void beforeTextChanged(CharSequence s, int start, int count, int after) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onTextChanged(CharSequence s, int start, int before, int count) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; marca = edMarca.getText().toString().trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chasis = edChasis.getText().toString().trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color = edColor.getText().toString().trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kilometraje = edKilometraje.getText().toString().trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modelo = edModelo.getText().toString().trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; placa = edPlaca.getText().toString().trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; motor = edMotor.getText().toString().trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; precio = edPrecio.getText().toString().trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; referencia = edReferencia.getText().toString().trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sucursal = edSucursal.getText().toString().trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!chasis.isEmpty()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && !color.isEmpty()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && !kilometraje.isEmpty()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &&!modelo.isEmpty()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && !placa.isEmpty()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && !motor.isEmpty()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &&!precio.isEmpty()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && !referencia.isEmpty()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && !sucursal.isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; admBotonGuardar.setVisibility(View.VISIBLE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; admBotonGuardar.setVisibility(View.GONE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void afterTextChanged(Editable s) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; edMarca.addTextChangedListener(adminTextwatcher);&nbsp; &nbsp; &nbsp; &nbsp; edChasis.addTextChangedListener(adminTextwatcher);&nbsp; &nbsp; &nbsp; &nbsp; edColor.addTextChangedListener(adminTextwatcher);&nbsp; &nbsp; &nbsp; &nbsp; edKilometraje.addTextChangedListener(adminTextwatcher);&nbsp; &nbsp; &nbsp; &nbsp; edModelo.addTextChangedListener(adminTextwatcher);&nbsp; &nbsp; &nbsp; &nbsp; edPlaca.addTextChangedListener(adminTextwatcher);&nbsp; &nbsp; &nbsp; &nbsp; edMotor.addTextChangedListener(adminTextwatcher);&nbsp; &nbsp; &nbsp; &nbsp; edPrecio.addTextChangedListener(adminTextwatcher);&nbsp; &nbsp; &nbsp; &nbsp; edReferencia.addTextChangedListener(adminTextwatcher);&nbsp; &nbsp; &nbsp; &nbsp; edSucursal.addTextChangedListener(adminTextwatcher);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {&nbsp; &nbsp; &nbsp; &nbsp; super.onActivityResult(requestCode, resultCode, data);&nbsp; &nbsp; &nbsp; &nbsp; if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Uri selectedImageUri = data.getData();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.i("ImageUri", "This is the ImageUri ----------------------->:" + selectedImageUri);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final StorageReference photoRef = storageReference.child(selectedImageUri.getLastPathSegment());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; photoRef.putFile(selectedImageUri).addOnSuccessListener(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this, new OnSuccessListener<UploadTask.TaskSnapshot>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Task<Uri> urlTask = taskSnapshot.getStorage().getDownloadUrl();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (!urlTask.isSuccessful()) ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final Uri downloadUrl = urlTask.getResult();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image = downloadUrl.toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.i("TAG", "This is the image:---->" + image);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void GuardarAutomoviles() {//&nbsp; &nbsp; &nbsp; &nbsp; if(image!=null) {&nbsp; &nbsp; &nbsp; &nbsp; String objMarca = edMarca.getText().toString().trim();&nbsp; &nbsp; &nbsp; &nbsp; int objChasis = Integer.parseInt(edChasis.getText().toString().trim());&nbsp; &nbsp; &nbsp; &nbsp; String objColor = edColor.getText().toString().trim();&nbsp; &nbsp; &nbsp; &nbsp; int objKilometraje = Integer.parseInt(edKilometraje.getText().toString().trim());&nbsp; &nbsp; &nbsp; &nbsp; int objModelo = Integer.parseInt(edModelo.getText().toString().trim());&nbsp; &nbsp; &nbsp; &nbsp; String objPlaca = edPlaca.getText().toString().trim();&nbsp; &nbsp; &nbsp; &nbsp; String objMotor = edMotor.getText().toString().trim();&nbsp; &nbsp; &nbsp; &nbsp; int objPrecio = Integer.parseInt(edPrecio.getText().toString().trim());&nbsp; &nbsp; &nbsp; &nbsp; String objReferencia = edReferencia.getText().toString().trim();&nbsp; &nbsp; &nbsp; &nbsp; String objSucursal = edSucursal.getText().toString().trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Automoviles autosAdmin = new Automoviles(objMarca, objPlaca, objReferencia, objColor, image,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objModelo, objPrecio, objChasis, objKilometraje, objMotor, objSucursal);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; databaseReference.push().setValue(autosAdmin);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String key = databaseReference.getKey();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(this, "save ok", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

米琪卡哇伊

您正在检查matches()哪个是错误的。你应该检查字符串对象.isEmpty()或者你也可以使用&nbsp;TextUtils.isEmpty(string)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java