gpt4 book ai didi

android - Android 自定义对话框中的日期选择器

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:31:13 25 4
gpt4 key购买 nike

我想在自定义对话框中使用日期选择器。在按钮上单击日历将打开为用户选择日期。我的 customDilaog 类中有 Button,在该按钮上单击我想打开日历 View 。如果单击此按钮,我的应用程序会崩溃。我已经完成了。

自定义对话框.java

public class CustomDialog extends Dialog implements android.view.View.OnClickListener {

private Button date;
DateFormat format = DateFormat.getDateInstance();
Calendar calender = Calendar.getInstance();

public CustomDialog(Context context) {
super(context);
}

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
date=(Button)findViewById(R.id.dateText2);
}
public void onClick(View v) {
if (v.getId()==R.id.datePick) {
openDatePicker(v);
//another if-else statements
}

public void updateDate(){
date.setText(format.format(calender.getTime()));
}

public void setDate(){
new DatePickerDialog(getContext(),dp,calender.get(Calendar.YEAR) ,calender.get(Calendar.MONTH),calender.get(Calendar.DAY_OF_MONTH)).show();
}

DatePickerDialog.OnDateSetListener dp = new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
calender.set(Calendar.YEAR,year);
calender.set(Calendar.MONTH, monthOfYear);
calender.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDate();
}
};
}

我不知道我哪里弄错了。

最佳答案

适本地使用它

 @OnClick(R.id.fab)
public void fabbuttonclicked(){
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(context.getContext());

View promptView = layoutInflater.inflate(R.layout.dialog_add_new_book, null);


final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context.getContext());

// set prompts.xml to be the layout file of the alertdialog builder
alertDialogBuilder.setView(promptView);

final EditText titlebookEditText = (EditText) promptView.findViewById(R.id.book_title_edit_text);
final EditText authorbookEditText=(EditText) promptView.findViewById(R.id.book_author_edit_text);
final Button dateboughtbookButton=(Button) promptView.findViewById(R.id.book_datebought_button);
final EditText conditionbookEditText=(EditText) promptView.findViewById(R.id.book_condition_edit_text);
final EditText urlimagebookEditText=(EditText) promptView.findViewById(R.id.book_Image_edit_text);

// Show a datepicker when the dateButton is clicked
dateboughtbookButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Calendar now = Calendar.getInstance();
final Calendar c = Calendar.getInstance();

DatePickerDialog dpd = new DatePickerDialog(context.getContext(),
new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
dateboughtbookButton.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);

}
}, c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DATE));
dpd.show();


}
}

);


alertDialogBuilder
.setCancelable(false)
.

setPositiveButton("Add Alarm",new DialogInterface.OnClickListener() {
@Override
public void onClick (DialogInterface dialog,int id){
String titlebook = titlebookEditText.getText().toString();
String authorbook = authorbookEditText.getText().toString();
String dateboughtbook = dateboughtbookButton.getText().toString();
String conditionbook = conditionbookEditText.getText().toString();
String urlimagebook = urlimagebookEditText.getText().toString();


if (titlebook.isEmpty()) {
Toast.makeText(getActivity(), " Amount is required ! \n Alarm not set", Toast.LENGTH_LONG).show();
return;
} else {
int nextID;
nextID = (int) (realm.where(Book.class).maximumInt("bookId") + 1);
Book book = new Book(nextID, titlebook, authorbook, dateboughtbook, conditionbook, urlimagebook);

realm.beginTransaction();
realm.copyToRealmOrUpdate(book);
realm.commitTransaction();

dialog.dismiss();
loadBooks();
Toast.makeText(getActivity(), " New BOOK added ! \n ", Toast.LENGTH_LONG).show();

//Toast.makeText(getActivity(), "Alarm is Set", Toast.LENGTH_LONG).show();
}
}
}

)
.

setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick (DialogInterface dialog,int id){
dialog.cancel();
}
});

// create an alert dialog
AlertDialog alertD = alertDialogBuilder.create();

alertD.show();

}

关于android - Android 自定义对话框中的日期选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30987181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com