英文:
How to fix an error when pressing button in pop-up
问题
I don't know what I am doing wrong, I have a pop up with a button that should take the value of an edit text but when it takes the value and save it in a string it closes, probing to get the text and put a confirm.setVisibility (View.VISIBILITY); but any changes you make to the xml are closed, it is also placed by placing a btnReset.setOnClickListener () on the button; but that only causes it to close as soon as I open the pop up.
This is the xml of the pop up:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="Cerrar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="15dp"
android:paddingTop="10dp"
android:paddingRight="15dp">
<ImageButton
android:id="@+id/botonCerrar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:background="#E6E6E6"
android:contentDescription="@string/imagen"
android:onClick="Cerrar"
app:srcCompat="@drawable/ic_cerrar" />
<TextView
android:id="@+id/info_popResetP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/info_popResetP"
android:textColor="@color/colorBlack"
android:textSize="18sp" />
<EditText
android:id="@+id/correo_popResetP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="@string/correo_login"
android:importantForAutofill="no"
android:inputType="textPersonName" />
<Button
android:id="@+id/botonReset"
android:layout_width="217dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="@drawable/b_morado_base"
android:onClick="BtnReset"
android:text="@string/reset_popResetP"
android:textColor="@color/colorWhite" />
<TextView
android:id="@+id/terminar_popResetP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foregroundGravity="center"
android:textColor="@color/colorAccent"
android:visibility="invisible" />
</LinearLayout>
</ScrollView>
This is the java code of the pop up:
private TextView confirmar;
private EditText correo;
private Button btnReset;
private String sCorreo = "";
private FirebaseAuth fireAuth;
private ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
confirmar = (TextView) findViewById(R.id.terminar_popResetP);
correo = (EditText) findViewById(R.id.correo_popResetP);
btnReset = (Button) findViewById(R.id.botonReset);
fireAuth = FirebaseAuth.getInstance();
pDialog = new ProgressDialog(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_reset_password);
DisplayMetrics medidas = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(medidas);
int ancho = medidas.widthPixels;
int alto = medidas.heightPixels;
getWindow().setLayout((int)(ancho*0.85), (int)(alto*0.5));
}
public void Cerrar(View view){
finish();
}
public void BtnReset(View view){
//NOTE: SOMETHING ERROR HAPPENS WHEN TAKING THE TEXT FROM THE EDIT TEXT OR WHEN A CHANGE IS MADE TO SOME ELEMENT OF THE XML LIKE CHANGING VISIBILITY.
confirmar.setText("@string/good_popResetP");
confirmar.setVisibility(View.VISIBLE);
if(!sCorreo.isEmpty() || sCorreo.contains("@") || sCorreo.contains(".")){
confirmar.setVisibility(View.INVISIBLE);
pDialog.setMessage("Verificando existencia de correo...");
pDialog.setCanceledOnTouchOutside(false);
pDialog.show();
ResetPassword();
}else {
confirmar.setText("Ingrese correo electronico");
confirmar.setVisibility(View.VISIBLE);
}
}
public void ResetPassword (){
fireAuth.setLanguageCode("es");
fireAuth.sendPasswordResetEmail(sCorreo).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
confirmar.setText("@string/good_popResetP");
}else {
confirmar.setText("@string/bad_popResetP");
}
pDialog.dismiss();
}
});
}
英文:
I don't know what I am doing wrong, I have a pop up with a button that should take the value of an edit text but when it takes the value and save it in a string it closes, probing to get the text and put a confirm.setVisibility (View.VISIBILITY); but any changes you make to the xml are closed, it is also placed by placing a btnReset.setOnClickListener () on the button; but that only causes it to close as soon as I open the pop up.
This is the xml of the pop up:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="Cerrar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="15dp"
android:paddingTop="10dp"
android:paddingRight="15dp">
<ImageButton
android:id="@+id/botonCerrar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:background="#E6E6E6"
android:contentDescription="@string/imagen"
android:onClick="Cerrar"
app:srcCompat="@drawable/ic_cerrar" />
<TextView
android:id="@+id/info_popResetP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/info_popResetP"
android:textColor="@color/colorBlack"
android:textSize="18sp" />
<EditText
android:id="@+id/correo_popResetP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="@string/correo_login"
android:importantForAutofill="no"
android:inputType="textPersonName" />
<Button
android:id="@+id/botonReset"
android:layout_width="217dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="@drawable/b_morado_base"
android:onClick="BtnReset"
android:text="@string/reset_popResetP"
android:textColor="@color/colorWhite" />
<TextView
android:id="@+id/terminar_popResetP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foregroundGravity="center"
android:textColor="@color/colorAccent"
android:visibility="invisible" />
</LinearLayout>
</ScrollView>
This is the java code of the pop up:
private TextView confirmar;
private EditText correo;
private Button btnReset;
private String sCorreo = "";
private FirebaseAuth fireAuth;
private ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
confirmar = (TextView) findViewById(R.id.terminar_popResetP);
correo = (EditText) findViewById(R.id.correo_popResetP);
btnReset = (Button) findViewById(R.id.botonReset);
fireAuth = FirebaseAuth.getInstance();
pDialog = new ProgressDialog(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_reset_password);
DisplayMetrics medidas = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(medidas);
int ancho = medidas.widthPixels;
int alto = medidas.heightPixels;
getWindow().setLayout((int)(ancho*0.85), (int)(alto*0.5));
}
public void Cerrar(View view){
finish();
}
public void BtnReset(View view){
//NOTE: SOMETHING ERROR HAPPENS WHEN TAKING THE TEXT FROM THE EDIT TEXT OR WHEN A CHANGE IS MADE TO SOME ELEMENT OF THE XML LIKE CHANGING VISIBILITY.
confirmar.setText("@string/good_popResetP");
confirmar.setVisibility(View.VISIBLE);
if(!sCorreo.isEmpty() || sCorreo.contains("@") || sCorreo.contains(".")){
confirmar.setVisibility(View.INVISIBLE);
pDialog.setMessage("Verificando existencia de correo...");
pDialog.setCanceledOnTouchOutside(false);
pDialog.show();
ResetPassword();
}else {
confirmar.setText("Ingrese correo electronico");
confirmar.setVisibility(View.VISIBLE);
}
}
public void ResetPassword (){
fireAuth.setLanguageCode("es");
fireAuth.sendPasswordResetEmail(sCorreo).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
confirmar.setText("@string/good_popResetP");
}else {
confirmar.setText("@string/bad_popResetP");
}
pDialog.dismiss();
}
});
}
答案1
得分: 0
findViewById(....)
只有在将视图绑定到类时才有效。
正确的方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
fireAuth = FirebaseAuth.getInstance();
pDialog = new ProgressDialog(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_reset_password);
confirmar = (TextView) findViewById(R.id.terminar_popResetP);
correo = (EditText) findViewById(R.id.correo_popResetP);
btnReset = (Button) findViewById(R.id.botonReset);
DisplayMetrics medidas = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(medidas);
int ancho = medidas.widthPixels;
int alto = medidas.heightPixels;
getWindow().setLayout((int)(ancho*0.85), (int)(alto*0.5));
}
英文:
findViewById(....) is only work when you bind the view to the Class
Correct way:
@Override
protected void onCreate(Bundle savedInstanceState) {
fireAuth = FirebaseAuth.getInstance();
pDialog = new ProgressDialog(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_reset_password);
confirmar = (TextView) findViewById(R.id.terminar_popResetP);
correo = (EditText) findViewById(R.id.correo_popResetP);
btnReset = (Button) findViewById(R.id.botonReset);
DisplayMetrics medidas = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(medidas);
int ancho = medidas.widthPixels;
int alto = medidas.heightPixels;
getWindow().setLayout((int)(ancho*0.85), (int)(alto*0.5));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论