如何在没有取消按钮的情况下创建Android时间戳?我原以为setCancelable(false)会这样做,但是没有摆脱按钮,它似乎只是防止取消点击窗口外面或后退按钮。
答案 0 :(得分:3)
如果您想使用TimePickerDialog,则非常简单。您只需要检测对话框何时显示,查找NEGATIVE按钮,因为它提供“取消”按钮,并将其可见性设置为GONE。如下所示:
final TimePickerDialog timePicker = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time
}
}, 12, 25, true); // 12 is the hour and 25 is minutes..please change this
timePicker.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
// This is hiding the "Cancel" button:
timePicker.getButton(Dialog.BUTTON_NEGATIVE).setVisibility(View.GONE);
}
});
timePicker.show();
答案 1 :(得分:0)
如果您不想要时间选择器的取消按钮,最好在xml文件中使用时间选择器,如:
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
你的活动应该是:
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));
}
}
这是供参考,希望这对你有帮助..谢谢:)