}
@Override
public void handleMessage(Message msg) { super.handleMessage(msg); if (!mStopped){ updateClock(); post(); } }
public void startScheduleUpdate(){ mStopped = false; post(); }
public void stopScheduleUpdate(){ mStopped = true; removeMessages(0); } }
private void updateClock() {
Calendar calendar=Calendar.getInstance();
int hour=calendar.get(Calendar.HOUR_OF_DAY); int minute=calendar.get(Calendar.MINUTE); timeString=hour+\ textView.setText(timeString); } }
实现界面:
2.编写一个 Android 应用。要求应用模拟系统登录界面效果,当用户输入正确的用户名和密码后,(设用户名为 admin;密码 123),再单击“确定”按钮,进入主界面,否则,给予相应的错误提示(Toast 方式)。界面设计示意图如图 1、图 2 所示(20 分) 提示:自建一个工程 exam2,包名: com.test.姓名拼音.exam2
请输入用户名 Admin,你好
请输入密码
欢迎使用本系统 记住密码 确定 图1登录Activity 图2系统主Activity
布局代码:
activity_main.xml:
android:id=\ android:layout_width=\ android:layout_height=\ android:gravity=\ android:textSize=\ android:text=\你好\ android:layout_width=\ android:layout_height=\ android:layout_below=\ android:gravity=\ android:textSize=\ android:text=\欢迎使用本系统\
login.xml:
android:id=\ android:layout_width=\ android:layout_height=\ android:hint=\请输入用户名\ android:id=\ android:layout_width=\ android:layout_height=\ android:layout_below=\ android:hint=\请输入密码\ android:inputType=\ android:id=\ android:layout_width=\ android:layout_height=\ android:layout_below=\ android:text=\记住密码\
实现代码:
package com.example.exam;
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View;
import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast;
public class LoginActivity extends Activity{ private EditText userName; private EditText password; private CheckBox rememberPsd; private Button submit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initUI(); initEvent(); } private void initEvent() { submit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String name = userName.getText().toString().trim(); String psd = password.getText().toString().trim(); if (name.equals(\ if (psd.equals(\ Intent intent = new Intent(getApplicationContext(),
MainActivity.class); startActivity(intent); } else { Toast.makeText(getApplicationContext(), \密码错误\ Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(getApplicationContext(), \用户名错误\ Toast.LENGTH_SHORT).show(); } } }); } @Override protected void onPause() { this.finish(); super.onPause(); } private void initUI() { setContentView(R.layout.login); userName = (EditText) findViewById(R.id.et_userName); password = (EditText) findViewById(R.id.et_password); rememberPsd = (CheckBox) findViewById(R.id.cb_rememberPsd); submit = (Button) findViewById(R.id.btn_submit); } }
实现界面: