我正在开发一个简单的应用程序来收集有关股票的最新信息。我正在应用AsyncTask并且我已经构建了我的GUI但是在我将Edit股票符号输入到EditText之后似乎没有任何事情发生。非常感谢任何帮助。我试过谷歌搜索这些主题,看看文档和教程,但不知道我错过了什么。我大约2个月的时间学习android开发。
这是我的代码:
package cornez.com.stockquotes;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
EditText editText;
String symbol;
TextView symbolText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView nameText, tradePriceText, tradeTimeText, changeText, rangeText;
symbolText = (TextView) findViewById(R.id.symbolText);
nameText = (TextView) findViewById(R.id.nameText);
tradePriceText = (TextView) findViewById(R.id.tradePriceText);
tradeTimeText = (TextView) findViewById(R.id.tradeTimeText);
changeText= (TextView) findViewById(R.id.changeText);
rangeText = (TextView) findViewById(R.id.rangeText);
editText = (EditText) findViewById(R.id.editText);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event)
{
if (actionId == EditorInfo.IME_ACTION_DONE)
{
// actions when "Done" key is pressed
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true;
}
return false;
}
});
editText.requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams.
SOFT_INPUT_STATE_VISIBLE);
getStockInfoTask stockTask = new getStockInfoTask();
symbol = editText.getText().toString();
stockTask.execute(symbol);
}
//AsyncTask
private class getStockInfoTask extends AsyncTask<String, Void, Stock>
{
protected Stock doInBackground(String... params)
{
Stock stock = new Stock(symbol);
try {
stock.load();
} catch (IOException e) {
e.printStackTrace();
}
return stock;
}
protected void onPostExecute(Stock stock)
{
symbolText.setText(stock.getSymbol());
}
}
}
答案 0 :(得分:0)
中的代码
onEditorAction
在新线程中运行。
代码
symbol = editText.getText().toString();
运行同步。
所以:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event)
{
if (actionId == EditorInfo.IME_ACTION_DONE)
{
// actions when "Done" key is pressed
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
getStockInfoTask stockTask = new getStockInfoTask();
symbol = editText.getText().toString();
stockTask.execute(symbol);
return true;
}
return false;
}
});
editText.requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams.
SOFT_INPUT_STATE_VISIBLE);