Android 使用AsyncTask刷新游戏分数文本视图
我正在用Android编写一个棋盘游戏,其中UI包含分数的文本视图(CPUScore和PlayerScore)。我遇到的问题是,当调用onCreate时,UI不会从其初始值更新分数。我已经研究过类似的问题,建议最多的解决方案是使用AsyncTask在后台更新UI线程。然而,我并没有找到一个解决方案来明确处理如何在AsyncTask中使用TextView 以下是我的尝试:Android 使用AsyncTask刷新游戏分数文本视图,android,user-interface,textview,android-asynctask,updating,Android,User Interface,Textview,Android Asynctask,Updating,我正在用Android编写一个棋盘游戏,其中UI包含分数的文本视图(CPUScore和PlayerScore)。我遇到的问题是,当调用onCreate时,UI不会从其初始值更新分数。我已经研究过类似的问题,建议最多的解决方案是使用AsyncTask在后台更新UI线程。然而,我并没有找到一个解决方案来明确处理如何在AsyncTask中使用TextView 以下是我的尝试: @Override protected void onCreate(Bundle savedInstanceState
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//....
setContentView(R.layout.main_layout);
//.....
//------------ textViews declared here don't refresh -------------------
TextView playerScoreForm = (TextView) findViewById(R.id.PlayerTotalScore);
playerScoreForm.setText(Integer.toString(PlayerTotal));
playerScoreForm.invalidate();
TextView CPUScoreForm = (TextView) findViewById(R.id.CPUTotalScore);
CPUScoreForm.setText(Integer.toString(CPUTotal));
CPUScoreForm.invalidate();
//--------------------------------------------------------------------------
//AsyncTask method:
new updatePlayerScore().execute(PlayerTotal);
new updateCPUScore().execute(CPUScoreForm);
}
AsyncTask子类:
private class updatePlayerScore extends AsyncTask<TextView, Void, Void> {
@Override
protected TextView doInBackground(TextView... params) {
// what to put here??
}
return playerScoreForm;
}
@Override
protected void onProgressUpdate(Integer... values) {
//??
}
protected void onPostExecute(Integer result) {
playerScoreForm.setText(Integer.toString(result));
}
}
private class UpdateCPUScore extends AsyncTask<TextView, Integer, Integer> {
// same syntax as updatePlayerScore
}
私有类updatePlayerScore扩展异步任务{
@凌驾
受保护的TextView doInBackground(TextView…参数){
//在这里放什么??
}
返回球员核心形态;
}
@凌驾
受保护的void onProgressUpdate(整型…值){
//??
}
受保护的void onPostExecute(整数结果){
playerCoreform.setText(Integer.toString(result));
}
}
私有类UpdateCPUScore扩展了AsyncTask{
//与updatePlayerScore的语法相同
}
问题:
如何将我在onCreate方法中声明的TextView传输到AsyncTask方法?我被难住了。我对Android开发相当陌生 a)我非常确定,在设置文本视图之后,不需要使它们无效;安卓应该自动做到这一点
b) 理论上,您应该将TextView
引用设置为成员变量,然后在onPostExecute
中引用它们,而不是将它们传递到doInBackground
doInBackground
将依次获取使您能够计算新分数的任何数据位。您在doInBackground
上所做的是任何会导致计算新分数的操作。doInBackground
的返回值被传递到onPostExecute
。然后在onPostExecute
中使用此数据更新TextView(现在是一个成员变量)。这有意义吗?实际上,您还没有在这里发布任何更新这些分数值的代码
有关快速示例,请参见
private TextView myScoreView; //initialized in onCreate as you do above.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//....
setContentView(R.layout.main_layout);
//.....
myScoreView = (TextView) findViewById(R.id.PlayerTotalScore);
myScoreView.setText(Integer.toString(PlayerTotal));
new updatePlayerScore().execute(1,2); //parameters for calculation
}
private class updatePlayerScore extends AsyncTask<Integer, Integer, Integer> {
@Override
protected TextView doInBackground(Integer... params) {
int score = params[0] + 2 * params[1];
return score;
}
@Override
protected void onProgressUpdate(Integer... values) {
//if you want to provide some indication in the UI that calculation
//is happening, like moving a progress bar, that's what you'd do here.
}
@Override
protected void onPostExecute(Integer scoreCalculationResult) {
myScoreView.setText(Integer.toString(scoreCalculationResult));
}
}
或:
您可以在
AsyncTask
的构造函数中传递TextView
,并通过onPostExecute
方法进行更新
private class updatePlayerScore extends AsyncTask<Void, Void, Integer> {
private TextView view;
public updatePlayerScore(TextView textView){
this.view = textView;
}
@Override
protected Integer doInBackground(Void... params) {
int score = 0;
//do you calculation the
return score;
}
protected void onPostExecute(Integer result) {
view.setText(Integer.toString(result));
}
}
私有类updatePlayerScore扩展异步任务{
私有文本视图;
public updatePlayerScore(TextView TextView){
this.view=textView;
}
@凌驾
受保护的整数doInBackground(Void…params){
智力得分=0;
//你计算这个数字吗
返回分数;
}
受保护的void onPostExecute(整数结果){
view.setText(Integer.toString(result));
}
}
注意:如果您的活动配置因任何原因发生更改,即用户旋转设备,而您的
AsyncTask
任务尚未完成,则不会更新您的TextView
更新,因此您应该保留AsyncTask的一个实例,并更新TextViewmakeplayerScoreForm
等类成员/将其传递到自定义异步任务的构造函数或参数中。请给我一个粗略的示例,说明如何执行此操作?谢谢您的评论。在游戏过程中,游戏逻辑会更新public int PlayerTotal和public int CPUTotal的分数。我的问题是如何转换:TextView playerCoreform=(TextView)findViewById(R.id.PlayerTotalScore);playerCoreform.setText(Integer.toString(PlayerTotal));到异步任务进程。在textView PlayerCoreform上执行setText时,PlayerTotal的更新值(从游戏循环更新)不会传递到UI线程。因此,在这里使用AsyncTask似乎不合适。我会更新我的答案。非常感谢。我使用了您的第一个编辑建议,并将其包含在一个void方法updatecores()中:public void updatecores(){rununuithread(new Runnable(){@Override public void run(){playerScoreForm.setText(Integer.toString(PlayerTotal));CPUScoreForm.setText(Integer.toString(CPUTotal));}}. 诀窍是从onCreate为初始分数调用它,然后从游戏中生成分数的方法调用它。感谢您的贡献。我的错误是,我试图从onCreate获取分数,但这是错误的,因为onCreate只会触发一次(即,当活动首次创建时),因此在执行onCreate时尝试在那里获取分数意味着分数保留了初始值,这些值在初始化时传递给onCreate。
myScoreView.post(new Runnable(){
@Override
public void run(){
myScoreView.setText(PlayerScoreValue);
}
});
private class updatePlayerScore extends AsyncTask<Void, Void, Integer> {
private TextView view;
public updatePlayerScore(TextView textView){
this.view = textView;
}
@Override
protected Integer doInBackground(Void... params) {
int score = 0;
//do you calculation the
return score;
}
protected void onPostExecute(Integer result) {
view.setText(Integer.toString(result));
}
}