每秒更新一个TextView一次,而应用程序正在运行 [英] Updating a TextView once per second while the App is Running
问题描述
我是新来的Android编程,但我很熟悉Java等等...
I am new to Android programming, but I'm quite familiar with Java and so...
在这个程序,我需要创造这样一个死亡/出生计数器和一个总人口计数器,所以我需要字符串在活动进行更新每一秒,但我不能看到找到任何解决办法。
In this app, I need to create something like a Death/Birth counter and a Total Population Counter, so I need the String at the Activity to be updated each second, but I can't seen to find any solution.
这是我的主要活动:
public class MainActivity extends ActionBarActivity {
static int nasc;
static int mortes;
static int popTotal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
nasc = 0;
mortes = 0;
popTotal = 0;
setContentView(R.layout.activity_main);
final TextView view = new TextView(this);
Boolean menu = false;
Handler handler = new Handler();
while (menu == false) {
handler.postDelayed(new Runnable() {
public void run() {
view.setText("Births : " + getNasc() + ".");
view.append("\n");
view.append("Deaths : " + getMortes() + ".");
view.append("\n");
view.append("Total Population : " + getPopTotal() + ".");
setContentView(view);
}
}, 1000);
}
}
private int getPopTotal() {
popTotal = nasc - mortes;
return popTotal;
}
private int getMortes() {
mortes = (int) (mortes + 1.7);
return mortes;
}
private int getNasc() {
nasc = nasc + 3;
return nasc;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.textView) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
有什么我已经做错了吗?
What have I been doing wrong?
推荐答案
您需要做的是去除while循环,只是如果菜单==虚假$ C重新运行了Runnable $ C>每一秒与新合计更新UI之后。
What you need to do is remove the while loop, and just re-run the Runnable if menu == false
each second after updating the UI with the new totals.
我刚刚得到这个实现通过了Runnable一个成员变量,而不是它是一个匿名类,用一个StringBuilder建立了TextView的文本,并重新运行了Runnable如果菜单工作
仍是在每次运行完成假的。
I just got this implementation working by making the Runnable a member variable instead of it being an anonymous class, using a StringBuilder to build up the text for the TextView, and re-running the Runnable if menu
is still false at the completion of each run.
public class MainActivity extends ActionBarActivity {
static int nasc;
static int mortes;
static int popTotal;
TextView view;
Handler handler;
Boolean menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nasc = 0;
mortes = 0;
popTotal = 0;
view = (TextView) findViewById(R.id.text);
menu = false;
handler = new Handler();
handler.postDelayed(updateText, 1000);
}
public Runnable updateText = new Runnable() {
@Override
public void run() {
StringBuilder sb = new StringBuilder();
sb.append("Births : " + getNasc() + ".");
sb.append("\n");
sb.append("Deaths : " + getMortes() + ".");
sb.append("\n");
sb.append("Total Population : " + getPopTotal() + ".");
view.setText(sb.toString());
if (menu == false){
handler.postDelayed(this, 1000);
}
}
};
//.................
activity_main.xml中:
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:id="@+id/text" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
这篇关于每秒更新一个TextView一次,而应用程序正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!