如何在 Android SDK 中使用 if/while 语句?
How can I use if/while statements in Android SDK?
我对 Android SDK 很陌生,所以这个问题可能很简单。
我尝试制作一个显示当前时间的应用程序。我知道如何获取当前时间,但不知何故我需要更新时间。所以我尝试放置一个 while/if 语句来更新 onCreate() 外部和 main Activity class 内部的时间,但是会弹出 4 个错误,提示“Decleration 意外结束。
我真的找不到解决这个问题的方法,所以我们将不胜感激。
我不确定您打算如何进行此操作,但请记住,您不会停止主线程。所以避免这样的结构:
while(true){ Thread.sleep(1000); updateTime(); }
更好的方法是使用 Handler,例如:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
updateTime();
}
}, 1000);
希望对您有所帮助 ;-)
我建议您使用 ScheduledThreadPoolExecutor class instead of a while
loop. There is an example in this tutorial 了解如何创建时钟应用程序。它可以作为一个有用的起点。
class ClockTask implements Runnable {
@Override
public void run() {
updateClock();
}
}
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
se.scheduleAtFixedRate(new ClockTask(), 0, 1000, TimeUnit.MICROSECONDS);
我认为更长的示例对您更有用。
为应用做布局,调用文件activity_main.xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.timedemo.MainActivity">
<TextView
android:id="@+id/textClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/textView" />
</LinearLayout>
创建一个名为 MainActivity.java 的文件,使其扩展 Activity(或扩展 Activity 的其他文件)
在 onCreate 方法中初始化布局。
使用处理程序 运行 延迟编码,而不是停止线程。
public class MainActivity extends AppCompatActivity {
//Handler can be used to send Runnables (code to run) to a specific Thread.
//In this case the UI-thread.
Handler handler = new Handler();
//TextView variable defined in Class-scope.
TextView myTextClock;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Load the layout from activity_main.xml into this Activity.
setContentView(R.layout.activity_main);
//Find the textclock that is mentioned in the activity_main.xml
//Use the ID to find the right View.
//As you can see in the xml-file, the id is 'textClock'.
//Looks like this in the XML --> <TextView android:id="@+id/textClock" />
myTextClock = (TextView) this.findViewById(R.id.textClock);
//Tell the Handler to execute this code at an interval.
handler.postDelayed(codeToRun, 1000);
}
//The runnable contains the code that will be run every second.
Runnable codeToRun = new Runnable() {
@Override
public void run() {
updateTime();
}
};
public void updateTime(){
//Code to update the Clock on the UI-thread
//see: http://developer.android.com/reference/java/text/SimpleDateFormat.html
DateFormat sdf = DateFormat.getDateTimeInstance();
myTextClock.setText(sdf.format(new Date()));
//Make sure it runs the next time too.
handler.postDelayed(codeToRun, 1000);
}
}
希望这可以帮助您走上正确的道路。
我对 Android SDK 很陌生,所以这个问题可能很简单。
我尝试制作一个显示当前时间的应用程序。我知道如何获取当前时间,但不知何故我需要更新时间。所以我尝试放置一个 while/if 语句来更新 onCreate() 外部和 main Activity class 内部的时间,但是会弹出 4 个错误,提示“Decleration 意外结束。
我真的找不到解决这个问题的方法,所以我们将不胜感激。
我不确定您打算如何进行此操作,但请记住,您不会停止主线程。所以避免这样的结构:
while(true){ Thread.sleep(1000); updateTime(); }
更好的方法是使用 Handler,例如:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
updateTime();
}
}, 1000);
希望对您有所帮助 ;-)
我建议您使用 ScheduledThreadPoolExecutor class instead of a while
loop. There is an example in this tutorial 了解如何创建时钟应用程序。它可以作为一个有用的起点。
class ClockTask implements Runnable {
@Override
public void run() {
updateClock();
}
}
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
se.scheduleAtFixedRate(new ClockTask(), 0, 1000, TimeUnit.MICROSECONDS);
我认为更长的示例对您更有用。
为应用做布局,调用文件activity_main.xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.timedemo.MainActivity">
<TextView
android:id="@+id/textClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/textView" />
</LinearLayout>
创建一个名为 MainActivity.java 的文件,使其扩展 Activity(或扩展 Activity 的其他文件)
在 onCreate 方法中初始化布局。
使用处理程序 运行 延迟编码,而不是停止线程。
public class MainActivity extends AppCompatActivity {
//Handler can be used to send Runnables (code to run) to a specific Thread.
//In this case the UI-thread.
Handler handler = new Handler();
//TextView variable defined in Class-scope.
TextView myTextClock;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Load the layout from activity_main.xml into this Activity.
setContentView(R.layout.activity_main);
//Find the textclock that is mentioned in the activity_main.xml
//Use the ID to find the right View.
//As you can see in the xml-file, the id is 'textClock'.
//Looks like this in the XML --> <TextView android:id="@+id/textClock" />
myTextClock = (TextView) this.findViewById(R.id.textClock);
//Tell the Handler to execute this code at an interval.
handler.postDelayed(codeToRun, 1000);
}
//The runnable contains the code that will be run every second.
Runnable codeToRun = new Runnable() {
@Override
public void run() {
updateTime();
}
};
public void updateTime(){
//Code to update the Clock on the UI-thread
//see: http://developer.android.com/reference/java/text/SimpleDateFormat.html
DateFormat sdf = DateFormat.getDateTimeInstance();
myTextClock.setText(sdf.format(new Date()));
//Make sure it runs the next time too.
handler.postDelayed(codeToRun, 1000);
}
}
希望这可以帮助您走上正确的道路。