android while命令,如何在Android SDK中使用if / while语句?

我认为更长的例子会对你更有用。

为应用程序制作布局,调用文件activity_main.xml。

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">

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" />

创建一个名为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 -->

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);

}

}

希望这可以帮助您走上正确的道路。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值