我正在寻找一种在我的应用中实现时间戳的方法。出于UX的原因,我不会使用任何对话框,因此我需要将它直接放在片段中。
我试图屠杀标准时间戳(非圆形时间戳),但效果不佳。
我正在寻找像三星时钟应用程序内部的东西: http://www.samsunggalaxys4.nl/wp-content/uploads/2013/08/samsung-galaxy-s4-snooze-sluimeren-instellen.jpg
只需向上和向下按钮,几位数。简单。
是的 - 我可以为自己编写代码,但是从我的经验来看,所有可视组件中都有很多微调,以使它们看起来和用户友好。说实话 - 我不是很擅长这种事情。
所以 - 你知道任何可以帮助我解决问题的来源吗?
答案 0 :(得分:0)
试试这个教程( http://www.tutorialspoint.com/android/android_timepicker_control.htm),我认为它可以帮到你:
的活动:
package com.example.timepicker;
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends Activity {
private TimePicker timePicker1;
private TextView time;
private Calendar calendar;
private String format = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
time = (TextView) findViewById(R.id.textView1);
calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int min = calendar.get(Calendar.MINUTE);
showTime(hour, min);
}
public void setTime(View view) {
int hour = timePicker1.getCurrentHour();
int min = timePicker1.getCurrentMinute();
showTime(hour, min);
}
public void showTime(int hour, int min) {
if (hour == 0) {
hour += 12;
format = "AM";
} else if (hour == 12) {
format = "PM";
} else if (hour > 12) {
hour -= 12;
format = "PM";
} else {
format = "AM";
}
time.setText(new StringBuilder().append(hour).append(" : ").append(min)
.append(" ").append(format));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
XML视图:
<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=".MainActivity" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/time_pick"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/set_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="180dp"
android:onClick="setTime"
android:text="@string/time_save" />
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/set_button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="24dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/timePicker1"
android:layout_alignTop="@+id/set_button"
android:layout_marginTop="67dp"
android:text="@string/time_current"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="@string/time_selected"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
字符串助手:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TimePicker</string>
<string name="action_settings">Settings</string>
<string name="time_picker_example">Time Picker Example</string>
<string name="time_pick">Pick the time and press save button</string>
<string name="time_save">Save</string>
<string name="time_selected"></string>
<string name="time_current">The Time is:</string>
</resources>
结果: