gpt4 book ai didi

java - 使用十字按钮清除两个编辑文本

转载 作者:行者123 更新时间:2023-12-01 18:31:10 25 4
gpt4 key购买 nike

我有一个应用程序,其中有登录表单,并且编辑文本末尾有一个十字按钮,但它们无法正常工作。我想要这样的事情:

enter image description here

电子邮件工作正常,但密码不起作用。我使用 Text Watcher 类进行这些操作,这是我的代码:

public class LoginActivity extends AppCompatActivity {

private EditText edEmail, edPassword;
private ImageButton cross_button_email, cross_button_password;
private TextView tvRegister;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

init();
gotoRegisterActivity();
}

private void init() {
edEmail = findViewById(R.id.ed_email);
edPassword = findViewById(R.id.ed_password);
cross_button_email = findViewById(R.id.imageButton_email);
cross_button_password = findViewById(R.id.imageButton_password);
tvRegister = findViewById(R.id.register_for_new_account);

edEmail.addTextChangedListener(textWatcher());
cross_button_email.setOnClickListener(onClickListener());

edPassword.addTextChangedListener(textWatcher());
cross_button_password.setOnClickListener(onClickListener());

cross_button_email.setVisibility(View.INVISIBLE);
cross_button_password.setVisibility(View.INVISIBLE);
}

private View.OnClickListener onClickListener() {
return new View.OnClickListener() {
@Override
public void onClick(View view) {
edEmail.setText(""); // clear editText
edPassword.setText("");
}
};
}

private void gotoRegisterActivity() {
tvRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent goToRegisterActivity = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(goToRegisterActivity);
}
});
}

private TextWatcher textWatcher() {
return new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
if (!edEmail.isActivated()) {
if (!edEmail.getText().toString().equals("")) {
cross_button_email.setVisibility(View.VISIBLE);
} else {
cross_button_email.setVisibility(View.GONE);
}
} else {
if (!edPassword.getText().toString().equals("")) {
cross_button_password.setVisibility(View.VISIBLE);
} else {
cross_button_password.setVisibility(View.GONE);
}
}
}

@Override
public void afterTextChanged(Editable editable) {

}
};
}

}

感谢任何帮助。

最佳答案

       cross_button_email.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
//to do //
edEmail.setText(""); //
/* you can also set flag when user On Click button */
}
});

关于java - 使用十字按钮清除两个编辑文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60164491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com