我是Android编程新手,但我对Java非常熟悉......
在这个应用程序中,我需要创建一个类似死亡/出生计数器和总人口计数器的东西,所以我需要每秒更新活动中的字符串,但我无法找到任何解决方案。
这是我的主要活动:
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);
}
}
我做错了什么?
答案 0 :(得分:2)
您需要做的是删除while循环,只需在使用新总计更新UI后每秒menu == false
重新运行Runnable。
我刚刚通过使Runnable成为一个成员变量而不是一个匿名类来实现这个实现,使用StringBuilder为TextView构建文本,并在menu
仍然是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);
}
}
};
//.................
的情况下重新运行Runnable每次运行结束时都是假的。
<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>
activity_main.xml中:
KeyValuePair<object, object>
答案 1 :(得分:0)
试试这个:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
nasc = 0;
mortes = 0;
popTotal = 0;
setContentView(R.layout.activity_main);
final TextView view = (TextView)findViewById(R.id.textView_id);
String allText="";
Boolean menu = false;
Handler handler = new Handler();
while (menu == false) {
handler.postDelayed(new Runnable() {
public void run() {
allText+="Births : " + getNasc() + ".";
allText+="\n";
allText+="Deaths : " + getMortes() + ".";
allText+="\n";
allTExt+="Total Population : " + getPopTotal() + ".";
view.setText(allText);
allText="";//reset the string
}
}, 1000);
}
}`
在textView声明中的activity_main.xml中添加
<TextView ...
... <!--other params that you have set-->
android:id="@+id/textview_id"
... />
答案 2 :(得分:0)
尝试这样帮助你
open System
let getValue (t: Type) (v: string) : obj =
if t = typeof<int> then box ((int) v)
elif t = typeof<byte> then box ((byte) v)
elif t = typeof<sbyte> then box ((sbyte) v)
elif t = typeof<int16> then box ((int16) v)
elif t = typeof<uint32> then box ((uint32) v)
elif t = typeof<int64> then box ((int64) v)
elif t = typeof<uint64> then box ((uint64) v)
elif t = typeof<double> then box ((double) v)
elif t = typeof<float32> then box ((float32) v)
elif t = typeof<decimal> then box ((decimal) v)
elif t = typeof<char> then box ((char) v)
elif t = typeof<string> then v :> obj
else
let s = sprintf "Error unknown type %A" t
raise (ApplicationException(s))
答案 3 :(得分:-1)
导入此:
//
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
//
并且每隔一分钟使用一次:
ScheduledExecutorService scheduleTaskExecut = Executors.newScheduledThreadPool(5);
// This run every one minute
scheduleTaskExecut.scheduleAtFixedRate(new Runnable() {
public void run() {
//in this Methos Update Your Text as You Need
UpdateYourText();
}
}, 0, 1, TimeUnit.MINUTES);