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

前端之家收集整理的这篇文章主要介绍了如何从android中的用户获取输入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在 android中有一个EditText,我希望用户输入文本并检查条件“BYE”

代码示例:

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

 }

如何让用户输入文本?UI应该等待输入文本(类似于我们在java应用程序中有InputStreamReader).

解决方法

我认为你不需要循环来做到这一点.从您的评论中看起来您还有一个“输入”按钮或您点击进行检查的内容.只需设置一个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);
                } });
原文链接:https://www.f2er.com/android/314284.html

猜你在找的Android相关文章