如何使用android TimePicker选择秒
我使用时间选择器让用户选择时间并查看。但我没有找到一种方法,让用户选择第二也。现在用户可以选择小时和分钟,但不能选择秒。如果我想把时间定在02:05:10,那我该怎么办?如何使用时间选择器选择第二个时间选择器?我认为最好的解决方案是使用时间类和三个数字选择器创建您自己的时间选择器。一种快速而肮脏的方法是使用两个时间选择器,一个用于小时、分钟,另一个用作秒数。将未使用的小时数隐藏在前几分钟下。这仅在24小时模式下有效。必须先声明秒的时间选择器,以便在下找到如何使用android TimePicker选择秒,android,custom-datetimepicker,Android,Custom Datetimepicker,我使用时间选择器让用户选择时间并查看。但我没有找到一种方法,让用户选择第二也。现在用户可以选择小时和分钟,但不能选择秒。如果我想把时间定在02:05:10,那我该怎么办?如何使用时间选择器选择第二个时间选择器?我认为最好的解决方案是使用时间类和三个数字选择器创建您自己的时间选择器。一种快速而肮脏的方法是使用两个时间选择器,一个用于小时、分钟,另一个用作秒数。将未使用的小时数隐藏在前几分钟下。这仅在24小时模式下有效。必须先声明秒的时间选择器,以便在下找到 <RelativeLayout
<RelativeLayout
<TimePicker
android:id="@+id/timePicker_Sec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="76dp" />
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/timePicker_Sec"
android:layout_marginLeft="0dp" />
</RelativeLayout>
我在GitHub上发布了一个开源项目,其中包含秒计时器:
看一看
最好的解决方法是创建三个独立的“数字选择器”,而不是时间选择器。。。我把它们放在一个线性布局中,效果很好
然后,您所要做的就是将值存储在三个单独的变量中。整数小时,整数分钟,整数秒,然后进行计算
这是我在不下载任何可能包含病毒的外部来源的情况下绕过它的方法
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<NumberPicker
android:id="@+id/numpicker_hours"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</NumberPicker>
<NumberPicker
android:id="@+id/numpicker_minutes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp">
</NumberPicker>
<NumberPicker
android:id="@+id/numpicker_seconds"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</NumberPicker>
</LinearLayout>
这是一个带有秒的自定义时间选择器对话框
MainActivity.java
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
final TextView timeTV = findViewById(R.id.time_text_view);
timeTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View view = View.inflate(MainActivity.this, R.layout.time_dialog, null);
final NumberPicker numberPickerHour = view.findViewById(R.id.numpicker_hours);
numberPickerHour.setMaxValue(23);
numberPickerHour.setValue(sharedPreferences.getInt("Hours", 0));
final NumberPicker numberPickerMinutes = view.findViewById(R.id.numpicker_minutes);
numberPickerMinutes.setMaxValue(59);
numberPickerMinutes.setValue(sharedPreferences.getInt("Minutes", 0));
final NumberPicker numberPickerSeconds = view.findViewById(R.id.numpicker_seconds);
numberPickerSeconds.setMaxValue(59);
numberPickerSeconds.setValue(sharedPreferences.getInt("Seconds", 0));
Button cancel = view.findViewById(R.id.cancel);
Button ok = view.findViewById(R.id.ok);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(view);
final AlertDialog alertDialog = builder.create();
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timeTV.setText(numberPickerHour.getValue() + ":" + numberPickerMinutes.getValue() + ":" + numberPickerSeconds.getValue());
// timeTV.setText(String.format("%1$d:%2$02d:%3$02d", numberPickerHour.getValue(), numberPickerMinutes.getValue(), numberPickerSeconds.getValue()));
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("Hours", numberPickerHour.getValue());
editor.putInt("Minutes", numberPickerMinutes.getValue());
editor.putInt("Seconds", numberPickerSeconds.getValue());
editor.apply();
alertDialog.dismiss();
}
});
alertDialog.show();
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/time_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
time_dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:gravity="center"
android:orientation="horizontal">
<NumberPicker
android:id="@+id/numpicker_hours"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<NumberPicker
android:id="@+id/numpicker_minutes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp" />
<NumberPicker
android:id="@+id/numpicker_seconds"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ffF0F0F0" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:text="Cancel"
android:textAllCaps="false" />
<View
android:id="@+id/view2"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#ffF0F0F0" />
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:text="OK"
android:textAllCaps="true" />
</LinearLayout>
</LinearLayout>
为此,我编写了一个Kotlin扩展函数。这很混乱,但它完成了任务:
/**
*以编程方式将秒数选择器添加到[TimePicker]格式中。
* */
有趣的时间选择器。addSeconds(){
val system=Resources.getSystem()
val idHour=system.getIdentifier(“hour”、“id”、“android”)
val idMinute=system.getIdentifier(“分钟”、“id”、“android”)
val idAmPm=system.getIdentifier(“amPm”、“id”、“android”)
val idLayout=system.getIdentifier(“timePickerLayout”、“id”、“android”)
val spinnerHour=作为数字picker的findViewById(idHour)
val spinnerMinute=findViewById(idMinute)作为数字图标
val spinnerAmPm=findViewById(idAmPm)作为数字签名器
val outerLayout=作为线性布局的findViewById(idLayout)
val layout=outerLayout.getChildAt(0)作为线性布局
layout.removeAllViews()
(spinnerAmPm.parent作为视图组)。移除视图(spinnerAmPm)
layout.addView(喷丝头小时)
setImeOptions(喷丝头小时,0)
layout.addView(喷丝头分钟)
设置时间选项(喷丝头分钟,1)
val spinnerSecond=createSecondPicker(spinnerHour.context)
layout.addView(第二喷丝头)
setImeOptions(spinnerAmPm,2)
val参数=喷丝头小时数。布局参数
spinnerSecond.layoutParams=参数
layout.addView(spinnerAmPm)
setImeOptions(spinnerAmPm,3)
}
private fun createSecondPicker(上下文:上下文):NumberPicker{
val spinnerSecond=NumberPicker(上下文)
喷丝头秒=R.id.second
spinnerSecond.setFormatter{i->String.format(“%02d”,i)}
spinnerSecond.minValue=0
spinnerSecond.maxValue=59
返回喷丝头秒
}
您还需要将此id添加到res/values/ids.xml
,以便可以使用R.id.second
在这篇帖子上大喊这个想法:谢谢,我必须创建我自己的picker视图。哦,Android,你这么频繁地让人失望……不管有没有黑客,这都是一个可行的解决方案,应该是Android平台本应支持的。荒谬。有时候简单是最好的。我在一个例子中使用了这个,并添加了冒号now.get(日历.小时),now.get(日历.分钟),now.get(日历.秒),true)
giving me现在无法解析符号
findViewById
返回的对象中如何不NPE,除非您使用全息主题构造时间选择器,使其使用微调器而不是时钟?