gpt4 book ai didi

java - Android 从 EditText 检索数据

转载 作者:行者123 更新时间:2023-12-02 06:41:23 24 4
gpt4 key购买 nike

我是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?

最佳答案

您需要向 EditText 添加一个监听器,以便在更改后检索其值。

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

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

请注意,您实际上并不需要实例变量,您可能想直接使用文本更改的值 - 这实际上归结为您是否需要在使用搜索方法处理它后重新使用它。在下面的示例中,我假设您在 Activity 中声明并分配了一个实例 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 的值将更新为该文本,您可以在代码中的其他位置使用它。

关于java - Android 从 EditText 检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19104651/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com