我是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_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
}
}除了文本字段之外,所有内容都可以正常工作。我浏览了一些类似的线程,我认为我的问题是,变量数在输入字段中的任何内容之前就得到了值,它总是选择默认文本,即使它显示的是不同的内容。我知道在我的代码中可能会有一些错误,但是现在我对解决这个特定的问题很感兴趣:我应该怎么做才能让我的变量数选择我在编辑文本字段中输入的值?如果不添加按钮或TextWatcher,是否有可能?
发布于 2013-09-30 22:16:57
您需要将侦听器添加到您的EditText中,以便在更改后检索其值。
您的“第一个问题”不应该是实际的问题(通过id检索EditText作为主Layout的子View ),只要在主Layout xml中声明了EditText,并且分配了这个id。
解决问题的最简单方法是将String变量声明为主Activity的实例属性。让我们称它为theText,并为它分配一个空的String。
请注意,您实际上并不需要实例变量,您可能想直接使用文本更改的值--这实际上取决于您是否需要在使用搜索方法处理它之后重用它。在下面的示例中,我假设您在您的String中声明和分配了一个实例theText变量。
将侦听器添加到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的值将被更新为该文本,您可以在代码的其他地方使用它。
发布于 2013-09-30 22:15:06
我找不到你在哪里输入的短信..。从您需要的编辑文本接收文本
String myText=edittext.getText().toString()其实你只需要edittext.getText().toString()但你明白重点..。我相信一旦你拿到它你就知道把它放在哪里了。
然后用它达到你想要的任何目的..。然后,如果需要字符串中的int,也可以使用Integer.parseInt(myText)作为Int获取它。
发布于 2014-07-16 13:59:10
从这条线,
EditText editText = (EditText) findViewById(R.id.enter_a_number);你刚刚看到了一个视图,EditText视图。现在你需要得到的数据,用户已经输入。若要执行此操作,请在此之后使用以下行
String eText=editText.getText().toString();https://stackoverflow.com/questions/19104651
复制相似问题