2010-09-22 17 views
6

我在Android上的EditText中,我希望用戶輸入的條件「再見」如何採取輸入從用戶的Android

代碼示例文本和檢查:

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一樣)。

回答

5

我不認爲你需要一個循環來做到這一點。從您的評論看來,您似乎也有一個「Enter」按鈕或您點擊進行檢查的內容。只需設置一個onclicklistener和的onclick可以使EditText上不可見(或不可編輯),檢查是的EditText等於「再見」,然後做你的行動可能會是這個樣子:

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); 
       } }); 
2

而不是在無限循環中運行此檢查,只能在EditText的每個onKeyUp上運行它。無論如何,無論如何,只有當用戶真正輸入了某些東西時,才能滿足條件。

+0

進入,但我想從keyboard.its像這樣輸入:我輸入一些文字,然後按Enter,然後同時循環還應當檢查如果輸入的字符串是「BYE」,則終止,否則進入循環並要求我進入反擊直到我進入「再見」 – SPB 2010-09-22 07:49:59

3

很簡單: -

EditText text = (EditText)findViewById(R.id.EditText01); 
String str = text.getText().toString(); 
現在海峽U將得到的字符串

這是在EditText上

相關問題