Home  >  Article  >  Java  >  Android learning series 1 uses buttons to display time

Android learning series 1 uses buttons to display time

高洛峰
高洛峰Original
2017-01-07 15:40:411396browse

Let’s first create a new project with AndroidStudio, select a blank template, and then drag in two Buttons and name their IDs as btDate (display date) and btTime (display time). His template XML code is very simple.

<?xml version="." encoding="utf-"?>
<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: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.neil.ad.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示当前日期"
android:id="@+id/btDate"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示当前时间"
android:id="@+id/btTime"
android:layout_below="@+id/btDate"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

A standard Android application window class needs to inherit the android.app.Activity class and at least implement the onCreate method to initialize the window. Next, implement the method

package com.neil.ad;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends Activity implements View.OnClickListener
{
private void showDialog(String title,String msg)
{
AlertDialog.Builder builder=new AlertDialog.Builder(this);
//设置对话框的图标
builder.setIcon(android.R.drawable.ic_dialog_info);
//设置对话框的标题
builder.setTitle(title);
//设置对话框的信息
builder.setMessage(msg);
//设置对话框的按钮
builder.setPositiveButton("确定",null);
//显示对话框
builder.create().show();
Intent intent;
}
//初始化窗口
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//装载了View(刚才编写的XML文件)
setContentView(R.layout.activity_main);
//获得两个按钮对象的实例
Button btDate=(Button)findViewById(R.id.btDate);
Button btTime=(Button)findViewById(R.id.btTime);
//为两个按钮添加单击事件的监听(实现了OnClickListener接口的对象)
btDate.setOnClickListener(this);
btTime.setOnClickListener(this);
}
//两个按钮共用一个单击事件,通过按钮的id区分单击了哪个按钮
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.btDate: {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//显示当前日期
showDialog("当前日期", sdf.format(new Date()));
break;
}
case R.id.btTime: {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
//显示当前日期
showDialog("当前时间", sdf.format(new Date()));
break;
}
}
}
}

Note:

1, AlertDialog can be used to display the dialog box

2, if multiple controls share an event method, it must be specified in the layout file The android:id attribute of the control label, and the id attribute of each control cannot be the same

3. Each resource file in the res (resource) directory will generate an int type in the R class in the gen directory Variable used to identify the current resource file. Therefore, in the onCreate method, the activity_main.xml file can be referenced through R.layout.activity_main, which means that a static int type variable called activity_main has been generated in the layout subclass of the R class. The code of the layout class

4. If you use a click event, you must implement the OnClickListener interface. The onClick method of this interface is the click event callback method.

Any window class in an Android application must be defined in the AndroidManifest.xml file, otherwise it cannot use. When defining the MainActivity class, the android:label attribute of the d3ae5c80fcb2a9903e7ea24877b1c6e7 label uses a string resource. The andrdoid:label attribute value used to define the 24fbed143a7d937464d286af420d7c51 label of Android application-related information also uses the string resource

The AndroidManifest.xml code is as follows

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.neil.ad01">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:label="@string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

It can also be used in string. Change the value of android:label in xml. The content of string.xml is as follows

<resources>
<string name="app_name">显示日期时间</string>
<string name="title_activity_main">显示日期时间</string>
</resources>

At this point, the code part is all written.

The emulator that comes with AndroidStudio says that you need to turn off the Hyper-v virtual machine in the Windows system and also need the HMAX intel accelerator. Genymotion cannot run even if it is installed. AndroidStudio cannot recognize it even if it installs domestic emulators. It’s really Halo+_+, fortunately QT is so powerful. You can use QT to develop Android based on C++ in the future. VS now also implements VC++ to develop Android. Now the interaction between various platforms is really becoming more and more powerful. Looking forward to greater breakthroughs! ! !


For more articles related to Android Learning Series 1 Using Buttons to Display Time, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn