如何在android中获取用户的输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3766900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 12:02:51  来源:igfitidea点击:

how to take input from user in android

android

提问by SPB

i have a EditText in android in which i want the user to enter the text and checks for the condition "BYE"

我在 android 中有一个 EditText,我希望用户在其中输入文本并检查条件“BYE”

Code sample:

代码示例:

EditText text = (EditText)findViewById(R.id.EditText01);
String abc= text .getText().toString(); 

while( !(abc).equals("bye")){

   abc = text.getText().toString();//user should enter the text from keyboard and the while loop should go and chech the condition.but not able to enter the text

   //do some operation with abc

 }

How can i make user to enter the text??The UI should wait for the text to be entered(something like we have InputStreamReader in java applications).

我怎样才能让用户输入文本??用户界面应该等待输入的文本(就像我们在 Java 应用程序中有 InputStreamReader 一样)。

采纳答案by CornCat

I don't think you need a loop to do this. From your comment it looks like you also have an "Enter" button or something that you click to do the checking. Just set an onclicklistener and onclick you can make the edittext invisible (or un-editable), check is the edittext is equal to "BYE" and then do your actions might look something like this:

我不认为你需要一个循环来做到这一点。从您的评论来看,您似乎还有一个“输入”按钮或单击以进行检查的东西。只需设置一个 onclicklistener 和 onclick 就可以使 edittext 不可见(或不可编辑),检查 edittext 是否等于“BYE”,然后执行您的操作可能如下所示:

final EditText ET = (EditText) findViewById(R.id.EnterText);
Button B1 = (Button) findViewById(R.id.EnterButton);
B1.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    ET.setVisibility(View.INVISIBLE);
                    if(ET.getText().toString() == "BYE")
                    {
                        //do something if it is "BYE"
                    } else {
                        Context context = getApplicationContext();
                    CharSequence text = "Please enter BYE";
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show(); 
                    }
                    ET.setVisibility(View.VISIBLE);
                } });

回答by Deepak Kumar 'SORTED'

Very Simple:-

很简单:-

EditText text = (EditText)findViewById(R.id.EditText01); 
String str = text.getText().toString(); 

now in str u will get string which is entered in EditText

现在在 str 中,您将获得在 EditText 中输入的字符串

回答by dxh

Instead of running this check in an infinite loop, only run it on every onKeyUpof the EditText. You know, anyway, that the condition will only ever be fulfilled if the user actually enters something.

不要在无限循环中运行此检查,而是仅在每个onKeyUpEditText上运行它。您知道,无论如何,只有在用户实际输入某些内容时才会满足条件。