首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >安卓从EditText中检索数据

安卓从EditText中检索数据
EN

Stack Overflow用户
提问于 2013-09-30 22:03:02
回答 5查看 9.7K关注 0票数 0

我是android程序的新手,所以请原谅我。这是我的第一个应用程序,它的目的是查找用户在sd卡上的txt中输入的搜索小部件中的字符串。我几乎完成了,但我的最后一个目标是添加一个功能,使用户可以决定有多少答案,他想得到的搜索。因此,我添加了一个EditText字段,可以在其中输入一个数字。这就是我的问题:我无法检索我在EditText字段中输入的数据,我也不知道为什么。这是我的onCreate。

代码语言:javascript
运行
复制
@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方法:

代码语言:javascript
运行
复制
   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
    }
}

除了文本字段之外,所有内容都可以正常工作。我浏览了一些类似的线程,我认为我的问题是,变量数在输入字段中的任何内容之前就得到了值,它总是选择默认文本,即使它显示的是不同的内容。我知道在我的代码中可能会有一些错误,但是现在我对解决这个特定的问题很感兴趣:我应该怎么做才能让我的变量数选择我在编辑文本字段中输入的值?如果不添加按钮或TextWatcher,是否有可能?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-09-30 22:16:57

您需要将侦听器添加到您的EditText中,以便在更改后检索其值。

您的“第一个问题”不应该是实际的问题(通过id检索EditText作为主Layout的子View ),只要在主Layout xml中声明了EditText,并且分配了这个id。

解决问题的最简单方法是将String变量声明为主Activity的实例属性。让我们称它为theText,并为它分配一个空的String

请注意,您实际上并不需要实例变量,您可能想直接使用文本更改的值--这实际上取决于您是否需要在使用搜索方法处理它之后重用它。在下面的示例中,我假设您在您的String中声明和分配了一个实例theText变量。

将侦听器添加到EditText中,如下所示:

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2013-09-30 22:15:06

我找不到你在哪里输入的短信..。从您需要的编辑文本接收文本

代码语言:javascript
运行
复制
String myText=edittext.getText().toString()

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

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

票数 1
EN

Stack Overflow用户

发布于 2014-07-16 13:59:10

从这条线,

代码语言:javascript
运行
复制
 EditText editText = (EditText) findViewById(R.id.enter_a_number);

你刚刚看到了一个视图,EditText视图。现在你需要得到的数据,用户已经输入。若要执行此操作,请在此之后使用以下行

代码语言:javascript
运行
复制
 String eText=editText.getText().toString();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19104651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档