java - Android:对话框中的时间选择器
问题描述
我试图修改我 time picker
在 stackoverflow 上找到的这个并将它放在dialog
我的application
. 当我尝试选择time
结果时,结果不是我设置的,而是设备的时间,所以时间设置得不好。TimeSettingfunction
用于调用时间选择器。
我试图让它从零开始并采取正确的时间但没有结果。有人可以帮我找出错误在哪里吗?
这是 ExampleDialog.java 文件:
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDialogFragment;
import com.ikovac.timepickerwithseconds.MyTimePickerDialog;
import com.ikovac.timepickerwithseconds.TimePicker;
import java.util.Calendar;
public class ExampleDialog extends AppCompatDialogFragment {
private EditText textInputNome;
private ExampleDialogListener listener;
private Button positiveButton;
private TextView text_view_timesec;
private Button SetTimeBTN;
private String nome = null;
private int timeInSeconds=-1;
private int hourOfDay;
private int minute;
private int seconds;
/*for MainActivity*/
public interface ExampleDialogListener {
boolean applyProgram(String nome);
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.layout_dialog, null);
textInputNome = view.findViewById(R.id.tiCrea);
SetTimeBTN=view.findViewById(R.id.btn_SetTime);
text_view_timesec=view.findViewById(R.id.textView3);
textInputNome.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
TimeSetting();
}
@SuppressLint("SetTextI18n")
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
nome =textInputNome.getText().toString().trim();
TimeSetting();
}
@Override
public void afterTextChanged(Editable s) {
TimeSetting();
}
});
builder.setView(view);
builder.setTitle("Nome programma");
builder.setNegativeButton("delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.setPositiveButton("create", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String nome = textInputNome.getText().toString().trim();
listener.applyProgram(nome);
}
});
Dialog dialog = builder.create();
return dialog;
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
listener = (ExampleDialogListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() +
"must implement ExampleDialogListener");
}
}
@Override
public void onDetach() {
super.onDetach();
listener = null;
}
@Override
public void onStart() {
super.onStart();
AlertDialog d = (AlertDialog) getDialog();
if (d != null ) {
positiveButton = d.getButton(Dialog.BUTTON_POSITIVE);
positiveButton.setEnabled(false);
}
}
private void TimeSetting(){
SetTimeBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "Btn clicked", Toast.LENGTH_SHORT).show();
Calendar c = Calendar.getInstance();
MyTimePickerDialog mTimePicker = new MyTimePickerDialog(getContext(), new MyTimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute, int seconds) {
SetTimeBTN.setText(getString(R.string.time) + String.format("%02d", hourOfDay) + ":" + String.format("%02d", minute) + ":" + String.format("%02d", seconds));
}
}, hourOfDay=c.get(Calendar.HOUR_OF_DAY), minute=c.get(Calendar.MINUTE), seconds=c.get(Calendar.SECOND), true);
timeInSeconds = ((hourOfDay * 60 * 60) + (minute * 60) + seconds);
text_view_timesec.setText("hh=" + hourOfDay + " mm=" + minute + " ss=" + seconds + " time tot=" + timeInSeconds);
positiveButton.setEnabled(!nome.isEmpty() && timeInSeconds > 0);
mTimePicker.show();
}
});
}
}
这是对话框的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/time_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tiCrea"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/name"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/btn_SetTime"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="186dp"
android:layout_height="wrap_content"
android:text="@string/clickToSetTime"
android:textAlignment="viewStart"
android:gravity="start" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="here is the time setted from the timepkr" />
</LinearLayout>
解决方案
在 onCreate 方法中更改这两行
final LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.layout_dialog, null);
至
final LinearLayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.layout_dialog, (ViewGroup)R.id.time_view");//here the id of the root LinearLayout in the XMl File layout_dialog.
推荐阅读
- datastage - 如何在数据阶段实现 not regexp_like()
- newrelic - New Relic 中的负 NRQL 平均值(diskFreeBytes)
- r - R - BigQuery:curl::curl_fetch_memory(url,handle = handle)中的错误:无法连接到 www.googleapis.com 端口 443:访问错误
- flutter-web - (Flutter) 如何识别用户在我的 ListView 中单击了 PopupMenuItem 的哪个 ListTile?
- node.js - 如何将 userId 与 post 表链接
- wpf - WPF:绑定到字典的每个组合框项目的唯一样式
- reactjs - 偶尔会出现在 useEffect() 中创建清理功能的警告
- javascript - 使用javascript拦截表单提交事件
- php - 如何使用额外条件提高 MySQL 查询的速度?
- for-loop - 如何使用在每个文件夹中重置的变量来批量重命名文件?