当前位置: 移动技术网 > IT编程>移动开发>Android > Android学习系列一用按钮实现显示时间

Android学习系列一用按钮实现显示时间

2019年07月24日  | 移动技术网IT编程  | 我要评论

戴旭震惊国人的演讲,第二型马尔科,我是歌手一共多少期

我们先用androidstudio新建一个项目,选择空白模板,然后像其中拖入两个button,将他们的id分别命名为btdate(显示日期),bttime(显示时间),他的模板xml代码很简单

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

如图所示


一个标准的android应用程序窗口类需要继承android.app.activity类,至少实现oncreate方法来初始化这个窗口。接下来实现方法

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

注:

1,alertdialog可用于显示对话框

2,如果多个控件共用一个事件方法,必须在布局文件中指定控件标签的android:id属性,并且每个控件的id属性不能相同

3.res(resource)目录中的每一个资源文件都会在gen目录下的r类中生成一个int类型的变量,用于标识当前资源文件。所以在oncreate方法中可以通过r.layout.activity_main引用activity_main.xml文件,这说明已经在r类的layout子类中生成了一个叫activity_main的静态int类型的变量,layout类的代码的代码如下

4,如果使用单击事件,必须实现onclicklistener接口,该接口的onclick方法就是单击事件回调方法

android应用程序中任何窗口类都必须在androidmanifest.xml文件中定义,否则无法使用。在定义mainactivity类时<activity>标签的android:label属性使用了字符串资源。用于定义android应用程序相关信息的<application>标签的andrdoid:label属性值也使用了字符串资源

androidmanifest.xml代码如下

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

其中还可以在string.xml中改变android:label的值,string.xml内容如下

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

至此,代码部分全部写完。

然后点击运行按钮,在模拟器中生成app,如图

androidstudio自带的模拟器说要关闭windows系统中的hyper-v虚拟机,还要hmax intel加速器,装genymotion也运行不了,装国内模拟器干脆androidstudio就识别不出来的,真是晕+_+,还好qt给力,以后还可以用qt基于c++开发android,vs现在也实现了vc++开发android,现在各种平台之间的交互真的是越来越给力了。期待更大的突破!!!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网