2013-09-30 74 views
0

我是android編程新手,請原諒。這是我的第一個應用程序,其目的是在用戶放置在SD卡上的txt中查找在搜索小部件中鍵入的字符串。我幾乎完成了它,但我最後的目標是添加一個功能,允許用戶決定在搜索後有多少答案。所以我添加了一個EditText字段,我可以鍵入一個數字。這是我的問題:我無法檢索我在EditText字段中輸入的數據,但我不知道爲什麼。這是我的創作。Android從EditText檢索數據

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    EditText editText = (EditText) findViewById(R.id.enter_a_number); // I think this is the firs part of my problem... 

    Intent intent = getIntent(); 
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
     String query = intent.getStringExtra(SearchManager.QUERY); 
     int number = Integer.valueOf(editText.getText().toString()); // ...and that is the second 
     do_my_search(query, number); 
    } 
} 

和do_my_search方法:

public void do_my_search(String query, int number) { 
     //Find the directory for the SD Card using the API 
    File sdcard = Environment.getExternalStorageDirectory(); 

    //Get the text file 
    File file = new File(sdcard,"fragment.txt"); 

    try { 

     BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "windows-1250"),8192); 
     String line; 
     String[] text = new String[5]; 
     int i = 0; 
     TextView textView1 = (TextView)findViewById(R.id.textView1); 
     textView1.setMovementMethod(new ScrollingMovementMethod()); 

     while ((line = br.readLine()) != null) { 
      if(line.toLowerCase().contains(query.toLowerCase()) == true && i < number){ 
       text[i] = i + 1 + "." + line + "\n"; 
       i++; 
      } 
     } 
     for(i = 0; i < number ; i ++){ 
      if(text[i] == null) 
       text[i] = ""; 
     } 
     StringBuilder builder = new StringBuilder(); 
     for (String value : text) { 
      builder.append(value); 
     } 
     String final_string = builder.toString(); 
     Spannable wordtoSpan = new SpannableString(final_string); 
     for(i = 0;;){ 
      int k = final_string.indexOf(query,i); 
      if (k == -1) 
       break; 
      wordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), k, k + query.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
      i = k + query.length(); 
     } 

     textView1.setText(wordtoSpan); 
     br.close(); 
    } 
    catch (IOException e) { 
     //You'll need to add proper error handling here 
    } 
} 

一切,但文本字段工作正常。我瀏覽過一些類似的線程,我認爲我的問題是變量數字在字段中輸入任何內容之前都會獲取值,即使它顯示了不同的內容,它也會選擇默認文本。我知道我的代碼中可能會出現一些錯誤,但現在我對解決這個特定問題感興趣:我應該如何讓我的可變數字選擇我在edittext字段中輸入的值?是否有可能沒有添加按鈕或TextWatcher?

+0

您可以發佈您activity_main.xml中的文件裏面? – Nick

+0

http://pastebin.com/pDtjmHqk –

回答

0

您需要爲您的EditText添加一個偵聽器,以便一旦更改就檢索其值。

你的「第一個問題」不應該是一個實際的問題(通過ID檢索EditText作爲孩子的主要LayoutView),只要EditText在主Layout XML聲明,以及是否爲其分配該ID。

解決您的問題最簡單的方法是聲明一個String變量作爲您的主Activity的實例屬性。我們稱之爲theText併爲其指定一個空的String

請注意,您並不是真的需要實例變量,您可能希望直接使用文本更改的值 - 這真的歸結爲您是否需要在使用搜索方法處理後重新使用該值,或者不。在下面的示例中,我假設您有一個實例String變量theText已在您的Activity中聲明和分配。

的偵聽器添加到您的EditText這樣:

editText.addTextChangedListener(new TextWatcher() { 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, 
      int after) { 
    } 

    @Override 
    public void afterTextChanged(Editable s) { 
     // we assign "theText" a value here 
     theText = s.toString(); 
     // we test it here (assuming your main Activity is called "MainActivity" 
     Toast.makeText(MainActivity.this, theText, Toast.LENGTH_LONG).show(); 
     // you can do whatever with the value here directly (like call "do_search"), 
     // or launch a background Thread to do it from here 
    } 
}); 

現在,一旦文本已經被用戶改變的theText值將被更新爲文本,你可以在你的代碼的其他地方使用它。

1

我無法找到你在哪裏得到輸入的文本....從你需要

String myText=edittext.getText().toString() 

的EditText上接收文本以及實際上你只需要edittext.getText().toString()但你明白了吧...我相信你知道一旦你得到它,它放在哪裏。

然後將其用於任何你想要的目的... 那麼你也可以把它作爲一個整數與Integer.parseInt(myText)如果你需要從字符串的Int。

0

使用edittext.getText().toString()從EditText獲取數據。它返回一個可以存儲在字符串變量中的字符串。

1

從這一行,

EditText editText = (EditText) findViewById(R.id.enter_a_number); 

你只是得到了一個視圖,視圖的EditText。現在您需要獲取用戶輸入的數據。要做到這一點,請使用以下行後

String eText=editText.getText().toString(); 
-1
EditText edtemail; 
String email; 

...那麼onCreate方法

edtemail=(EditText)findViewById(R.id.text_email); 
email=edtemail.getText.toString();