Reputation: 954
I have to implement a return method of a class. So the structure is as simple as
public boolean yesNo(String message){
//return something
}
The problem here is that there is an EditText object and a Button and I want return a boolean depending on the answer entered by the User in the EditText. So naturally I have to wait for the user to write and click the Button when he finishes. I already tried setting up an onClicklistener inside the method but then I would not be able to pass anything to the outer method.
I know that in android everything is asynchronous so I you are not supposed to wait for the user to do something but in this case I have no other idea. Also FYI the method that I'm implementing is of the UserInfo class of the Jsch library. From what i understood this UserInfo is designed for user interaction so I figured it would somehow manage "prompt" the user by calling the yesNo method and then "wait" for the user to react. Any ideas? Also I would be happy if anyone would explain to me if there is some error in my logic. Im not quite confident in using AsynTask or runnables but I think its got something to do with this.
The only available documentation of UserInfo class
Thanks a lot!
EDIT: code so far:
public static class MyUserInfo implements UserInfo, UIKeyboardInteractive {
public Button goButton = (Button) MainActivity.contentView.findViewById(R.id.startSSH);
public EditText editText = (EditText) MainActivity.contentView.findViewById(R.id.commandSSH);
public boolean yesNo = false;
public boolean promptYesNo(String str) {
goButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String answer = editText.getText().toString();
if(answer.equals("yes")){yesNo = true;}
else {yesNo = false;}
}
});
return yesNo;
}
}
Upvotes: 1
Views: 2410
Reputation: 12953
I already tried setting up an onClicklistener inside the method but then i would not be able to pass anything to the outer method.
What do you want to pass to the outer method? You can create class variables to make it available inside the whole class.
The promptYesNo method:
public void promptYesNo(String s) {
if(s.equals("Yes") //do something
else if(s.equals("No") //do something
}
Change your code this way:
public static class MyUserInfo implements UserInfo, UIKeyboardInteractive {
public Button goButton = (Button) MainActivity.contentView.findViewById(R.id.startSSH);
public EditText editText = (EditText) MainActivity.contentView.findViewById(R.id.commandSSH);
goButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if (prompt) {
promptYesNo(editText.getText().toString());
}
}
});
}
Upvotes: 1
Reputation: 2202
Put an OnClickListener
on the Button
when the screen is initialized, for example at onCreate
lifecycle method. At the listener's onClick
method you can call the yesNo
method with the EditText
content.
Try this way at the screen's onCreate
method:
mButton = (Button)findViewById(R.id.my_button);
mEditText = (EditText) findViewById(R.id.my_edittext);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
yesNo(mEditText.getText().toString());
}
});
Upvotes: 2