我正在尝试在条件为真的情况下获得一个按钮以转到下一个活动,但它不起作用



这段代码应该让我使用我制作的3个方法进入下一个活动。checkEditText方法应该取一个editText参数并将其更改为字符串,然后确保它不是空的。checkTextLetters应该采用editText参数,然后确保它只包含字母和/或空格。那么方法configureNextButton应该只在前面两个方法为真的情况下运行:

private boolean checkEditText(EditText text){
if(text.getText().toString().trim().length() > 0)
{
return true;
}
//try and print to screen "name is left blank//
return false;
}

我认为每当我在纯文本中键入asdf或其他内容时,此方法都会返回true

private boolean checkTextLetters(EditText text){
String line = text.getText().toString();
//checks to make sure that the string contains only the characters a-z and A-Z and/or spaces
boolean checkChars = line.matches("[a-zA-Z]");
boolean checkSpaces = line.matches("\s+");
if(checkChars && checkSpaces){
return true;
}
else if(checkChars){
return true;
}
return false;
}

这个方法应该只从我输入的纯文本中提取文本,并检查以确保它只包含字母和空格:

private void configureNextButton(boolean textCheck, boolean checkIfLetters){
//create if statement to not activate button if editText is empty
if(!textCheck || !checkIfLetters) {
return;
}
//will create variable 'mainButton' from the id of 'button' on MainActivity
Button mainButton = findViewById(R.id.button);
//sets the 'mainButton' to respond to a click
mainButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//states that when 'mainButton' is clicked, it begins Intent to switch to Main2Activity
startActivity(new Intent(MainActivity.this, Main2Activity.class));
}
});
}

这是按钮应该带我进入下一个活动的方法。我将"checkEditText"方法和"checkTextLetters"保存为布尔变量,并将它们作为该方法的参数传递,这样,如果它们等于true,则代码将让按钮将其带到下一个活动,如果其中任何一个是false,则它将不会执行任何操作:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editTextLines = findViewById(R.id.editText);
//call method to use 'button' to go to next activity given all conditions are true
boolean checkNotEmpty = checkEditText(editTextLines);
boolean checkIfLetters = checkTextLetters(editTextLines);
configureNextButton(checkNotEmpty,checkIfLetters);
}

这是我把它放在一起并运行它的主要方法。当我只把checkNotEmptycheckIfLetters设置为truefalse时,它就会工作。每当我试图通过调用我的两个方法来声明它们的值时,按钮就不会起任何作用。

我更改了代码,去掉了"checkEditText"one_answers"CheckTextLetters"方法,而不是主方法中的TextWatcher方法。它有效,但有错误。1.当它第一次运行时,我可以按下按钮,它将进入下一个活动。2.我可以输入任何字母和数字,但不会只接受数字。我希望EditText简单,如果它是空的,就不起作用,只允许字母和空格

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText = findViewById(R.id.editText);
final Button button = findViewById(R.id.button);
button.setClickable(false);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(s.toString().length() == 0)
button.setClickable(false);
else{
String line = s.toString();
boolean value = line.matches("[a-zA-Z]");
// set bool 'value' to check for alphabet letters
if(value)
button.setClickable(true);
}

}
});
//call method to use 'button' to go to next activity given all conditions are true
configureNextButton();

我认为您需要使用TextWatcher。

请参阅以下示例代码:用您的逻辑实现这一点,并将现有函数更改为接受String而不是EditText。

btnSubmit.setClickable(false);
edtText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(s.toString().length() == 0){
btnSubmit.setClickable(false);
}
else
{
//Validate if its Alphabets only - if returned true,
boolean value = validateAlphabetsOnly(s.toString());
if(value)
{
btnSubmit.setClickable(true);
}
}
}
});

onCreate中,活动刚刚创建。用户还没有任何机会在EditText中写作。因此,您需要将TextChangedListener用于EditText。

相关内容

最新更新