Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android从EditText检索数据_Java_Android_Android Edittext - Fatal编程技术网

Java Android从EditText检索数据

Java Android从EditText检索数据,java,android,android-edittext,Java,Android,Android Edittext,我是android编程新手,请原谅。这是我的第一个应用程序,它的目的是在sd卡上的txt中查找用户在搜索小部件中键入的字符串。我几乎完成了它,但我的最后一个目标是添加一个功能,允许用户在搜索后决定想要得到多少答案。所以我添加了一个EditText字段,在这里我可以键入一个数字。这就是我的问题:我无法检索我在EditText字段中键入的数据,我也不知道为什么。这是我的onCreate @Override public void onCreate(Bundle savedInstanceState)

我是android编程新手,请原谅。这是我的第一个应用程序,它的目的是在sd卡上的txt中查找用户在搜索小部件中键入的字符串。我几乎完成了它,但我的最后一个目标是添加一个功能,允许用户在搜索后决定想要得到多少答案。所以我添加了一个EditText字段,在这里我可以键入一个数字。这就是我的问题:我无法检索我在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_搜索方法:

   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
    }
}
public void do\u my\u搜索(字符串查询,整数){
//使用API查找SD卡的目录
文件sdcard=Environment.getExternalStorageDirectory();
//获取文本文件
File File=新文件(sdcard,“fragment.txt”);
试一试{
BufferedReader br=新的BufferedReader(新的InputStreamReader(新文件InputStream(文件),“windows-1250”),8192;
弦线;
字符串[]文本=新字符串[5];
int i=0;
TextView textView1=(TextView)findViewById(R.id.textView1);
textView1.setMovementMethod(新的ScrollingMovementMethod());
而((line=br.readLine())!=null){
if(line.toLowerCase().contains(query.toLowerCase())==true&&i

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

我找不到输入文本的地方。。。。要从edittext接收所需的文本

String myText=edittext.getText().toString()
实际上,您只需要
edittext.getText().toString()
,但您明白了。。。我相信你一拿到它就知道放在哪里了

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

您需要在
编辑文本中添加一个侦听器,以便在更改后检索其值

您的“第一个问题”不应该是实际问题(通过id作为主
版面
的子
视图
检索
编辑文本
),只要在主
版面
xml中声明了
编辑文本
,并分配了该id即可

解决问题的最简单方法是将
字符串
变量声明为主
活动
的实例属性。让我们将其称为
文本
,并为其分配一个空的
字符串

请注意,您实际上并不需要实例变量,您可能希望直接使用文本更改的值-这实际上归结为您是否需要在使用搜索方法处理它之后重新使用它。在下面的示例中,我假设您在
活动
中声明并分配了一个实例
字符串
变量
文本

将侦听器添加到您的
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
的值将更新为该文本,您可以在代码的其他地方使用它

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

从此行开始

 EditText editText = (EditText) findViewById(R.id.enter_a_number);
你刚刚得到一个视图,编辑文本视图。现在您需要获取数据,用户已输入。要执行此操作,请在该操作之后使用以下行

 String eText=editText.getText().toString();
。。。然后在oncreate方法中

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

你能发布你的activity_main.xml文件吗?
edtemail=(EditText)findViewById(R.id.text_email);
email=edtemail.getText.toString();