#android #android-timepicker
#Android #android-timepicker
Вопрос:
Друзья, я новичок в разработке приложений для Android. Я почти завершил свое приложение.
На последнем этапе я сталкиваюсь с трудностями и последние три дня пытаюсь решить эту проблему, но не смог.
Мне нужно получить текущие часы и минуты из средства выбора времени (без диалогового окна), когда пользователь нажимает кнопку. Я хочу сохранить эти данные в базе данных при нажатии этой кнопки.
Не могли бы вы, кто-нибудь, пожалуйста, помочь мне предоставить только файл XML и JAVA, как показано ниже.
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="com.example.timepicker.MainActivity" >
<TimePicker
android:id="@ id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="21dp" />
<Button
android:id="@ id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@ id/timePicker1"
android:layout_centerHorizontal="true"
android:layout_marginTop="77dp"
android:text="Button" />
</RelativeLayout>
Java:
package com.example.timepicker;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Ответ №1:
Используя экземпляр календаря, вы можете получить текущие часы и минуты-
Calendar c;
private int mhour;
private int mminute;
инициализировать —
c = Calendar.getInstance();
mhour = c.get(Calendar.HOUR);
mminute = c.get(Calendar.MINUTE);
// you will get current hour and minutes of device time
Также обратитесь к этому календарю