Android 从 EditText 检索数据



我是安卓编程的新手,所以请原谅。这是我的第一个应用程序,其目的是查找用户在txt中放置在SD卡上的搜索小部件中键入的字符串。我几乎完成了它,但我的最后一个目标是添加一个功能,允许用户决定搜索后他想要获得多少答案。所以我添加了一个编辑文本字段,我可以在其中键入一个数字。这就是我的问题:我无法检索我在 EditText 字段中键入的数据,我不知道为什么。这是我的onCreate。

@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中,以便在更改后检索其值。

您的"第一个问题"不应该是实际问题(通过 id 检索EditText作为主Layout的子View(,只要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的值将更新为该文本,您可以在代码中的其他位置使用它。

我找不到您输入的文本的位置....从您需要的编辑文本接收文本

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

好吧,实际上你只需要edittext.getText().toString()但你明白了重点......我相信一旦你得到它,你就知道把它放在哪里。

然后把它用于你想要的任何目的......然后,如果您需要字符串中的 Int Integer.parseInt(myText),您也可以将其作为 int 获取。

从这一行,

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

你刚刚得到了一个视图,编辑文本视图。现在你需要获取数据,用户已经输入。为此,请在该行之后使用以下行

 String eText=editText.getText().toString();

使用 edittext.getText().toString() 从 EditText 获取数据。它返回一个字符串,您可以将其存储在字符串变量中。

EditText edtemail;
String email;

。然后在 oncreate 方法中

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

最新更新