Monday, February 24, 2014

Ask a question again DatePickerDialog


I did a date chosen to have two buttons "OK" and "Cancel."
I want to achieve the effect of click "Cancel" to close the pop-up dialog box, select the date and press "OK" to close the pop-up dialog box while the date is written textview of the selected date.
But now no matter what I press "Cancel" or "OK", the date will be written in textview. How to solve?
"Cancel" and "OK" click event associated with onDateSet what?
There toast pop-up message if I write the report in onDateSet "Unfortunately, test01 has stopped." Mistake, this is why?

 
private DatePickerDialog.OnDateSetListener DatePickerListener = new DatePickerDialog.OnDateSetListener () {

@ Override
public void onDateSet (DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
/ / TODO Auto-generated method stub
textview = (TextView) findViewById (R.id.textview1);
textview.setText (year + "-" + monthOfYear + "-" + dayOfMonth);
}
};


 
DatePickerDialog picker = new DatePickerDialog (MainActivity.this, DatePickerListener,
iYear, iMonth, iDay);
picker.setCancelable (true);
picker.setCanceledOnTouchOutside (true);
picker.setButton (DialogInterface.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener () {
@ Override
public void onClick (DialogInterface dialog, int which) {
Log.d ("Picker", "Correct behavior!");
}
});
picker.setButton (DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener () {
@ Override
public void onClick (DialogInterface dialog, int which) {
Log.d ("Picker", "Cancel!");
}
});
picker.show ();
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
You put textview = (TextView) findViewById (R.id.textview1);
textview.setText (year + "-" + monthOfYear + "-" + dayOfMonth); into the "OK" button in Try
Reply:
cited a floor a394268045 reply:
you put textview = (TextView) findViewById (R.id.textview1);
textview.setText (year + "-" + monthOfYear + "-" + dayOfMonth); into the "OK" button in Try

Click OK in the event there are no incoming year, monthOfYear, dayOfMonth these three parameters
Reply:
reference to the second floor yiyamzen reply:
Quote: references to a floor a394268045 reply:

You put textview = (TextView) findViewById (R.id.textview1);
textview.setText (year + "-" + monthOfYear + "-" + dayOfMonth); into the "OK" button in Try

Click OK in the event there are no incoming year, monthOfYear, dayOfMonth these three parameters

Can onDateSet () parameter will be coming out, and then in the onClick () to handle the
Reply:
reference to the third floor a394268045 reply:
Quote: references to the second floor yiyamzen reply:

Quote: references to a floor a394268045 reply:

You put textview = (TextView) findViewById (R.id.textview1);
textview.setText (year + "-" + monthOfYear + "-" + dayOfMonth); into the "OK" button in Try

Click OK in the event there are no incoming year, monthOfYear, dayOfMonth these three parameters

Can onDateSet () parameter will be coming out, and then in the onClick () go deal

Such seemingly becomes complicated, ah, I read very simple, the effect is not the same as running-_-;;
Reply:
Many books online content on the Android seems to have become obsolete, people are depressed.
Reply:
My posts float to ~
Reply:
Why is it so
Reply:
Code is incomplete ah! Where the assignment date, all posted Come!
Reply:
Sorry, did not see. You can add a boolean flag,
@ Override
public void onDateSet (DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
/ / TODO Auto-generated method stub
if (flag) {
textview = (TextView) findViewById (R.id.textview1);
textview.setText (year + "-" + monthOfYear + "-" + dayOfMonth);
}
}
picker.setButton (DialogInterface.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener () {
@ Override
public void onClick (DialogInterface dialog, int which) {
Log.d ("Picker", "Correct behavior!");
flag = true;
}
});
picker.setButton (DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener () {
@ Override
public void onClick (DialogInterface dialog, int which) {
Log.d ("Picker", "Cancel!");
flag = false;
}
});

Reply:
What used book? ? I feel you write this demo problem!
Reply:
reference to the 9th floor pengguohua1988 reply:
sorry, did not see. You can add a boolean flag,

Thank you very much, this method you can.
But this appears to me to write a vegetable, I think there should not borrow DatePickerDialog other external variables solution. . .
I do not know if anyone would be standard generic approach.
Reply:
reference to the 10th floor u011133213 reply:
with what book? ? I feel you write this demo problem!

My book is a DatePicker control, is added directly to the Activity on, rather than press a button pops up.
I see the application on the phone all the pop-up thing, and then I found online the following method.
new DatePickerDialog (MainActivity.this, DatePickerListener, iYear, iMonth, iDay) show ();.
So when people are using the "OK" and "Cancel" buttons, and I use only one "OK" button.
Later, a well-intentioned people can use the Internet to tell me setButton method to add the Cancel button. But the problem I mentioned above plus onDateSet appear after it.
Reply:
reference to the 12th floor yiyamzen reply:
Quote: references to the 10th floor u011133213 reply:

What used book? ? I feel you write this demo problem!

My book is a DatePicker control, is added directly to the Activity on, rather than press a button pops up.
I see the application on the phone all the pop-up thing, and then I found online the following method.
new DatePickerDialog (MainActivity.this, DatePickerListener, iYear, iMonth, iDay) show ();.
So when people are using the "OK" and "Cancel" buttons, and I use only one "OK" button.
Later, a well-intentioned people can use the Internet to tell me setButton method to add the Cancel button. But the problem I mentioned above plus onDateSet appear after it.

I ask you to easily put your demo code directly sent for me, I can help you tune the next try! !

Reply:
@ Override
public void onDateSet (DatePicker view, int year, int monthOfYear,
int dayOfMonth) {

I understand this approach, DatePicker is selected the default value, so whether you're OK or Cancel, year, month, and day will be filled in textview in, because you fill in the contents of this method in the.


If the landlord to solve this problem, please share, thank Ha!
Reply:
reference to the 13th floor u011133213 reply:
ask you to easily put your demo code directly sent for me, I can help you tune the next try! !

Of course, wish for.

activity_main.xml
 
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 = ". MainActivity">

android: id = "@ + id/textview1"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: text = "@ string / hello_world" />




MainActivity.java
 
package com.example.test01;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
private NotificationManager notificationmanager;
private Notification notification;
private TextView textview;

private DatePickerDialog.OnDateSetListener DatePickerListener = new DatePickerDialog.OnDateSetListener () {

@ Override
public void onDateSet (DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
/ / TODO Auto-generated method stub

textview = (TextView) findViewById (R.id.textview1);
textview.setText (year + "-" + monthOfYear + "-" + dayOfMonth);
/ / Use the following procedure will quit Toast, puzzled.
/ / Toast.makeText (getApplicationContext (), year, Toast.LENGTH_SHORT) show ();.

}
};

@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);

final Calendar objTime = Calendar.getInstance ();
int iYear = objTime.get (Calendar.YEAR);
int iMonth = objTime.get (Calendar.MONTH);
int iDay = objTime.get (Calendar.DAY_OF_MONTH);

/ / New DatePickerDialog (MainActivity.this, DatePickerListener, iYear, iMonth, iDay) show ();.
DatePickerDialog picker = new DatePickerDialog (MainActivity.this, DatePickerListener,
iYear, iMonth, iDay);
picker.setCancelable (true);
picker.setCanceledOnTouchOutside (true);
picker.setButton (DialogInterface.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener () {
@ Override
public void onClick (DialogInterface dialog, int which) {
Log.d ("Picker", "Correct behavior!");
}
});
picker.setButton (DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener () {
@ Override
public void onClick (DialogInterface dialog, int which) {
Log.d ("Picker", "Cancel!");
}
});
picker.show ();


}

@ 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;
}

}

Reply:
reference to the 14th floor pengguohua1988 reply:
@ Override
public void onDateSet (DatePicker view, int year, int monthOfYear,
int dayOfMonth) {

I understand this approach, DatePicker is selected the default value, so whether you're OK or Cancel, year, month, and day will be filled in textview in, because you fill in the contents of this method in the.


If the landlord to solve this problem, please share, thank Ha!

Some people say that the Internet is that no matter the point "OK" or "Cancel" will tune onDateSet methods.

Reply:
cited 15 floor yiyamzen reply:
Quote: references to the 13th floor u011133213 reply:

I ask you to easily put your demo code directly sent for me, I can help you tune the next try! !

Of course, wish for.

activity_main.xml
 
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 = ". MainActivity">

android: id = "@ + id/textview1"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: text = "@ string / hello_world" />




MainActivity.java
 
package com.example.test01;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
private NotificationManager notificationmanager;
private Notification notification;
private TextView textview;

private DatePickerDialog.OnDateSetListener DatePickerListener = new DatePickerDialog.OnDateSetListener () {

@ Override
public void onDateSet (DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
/ / TODO Auto-generated method stub

textview = (TextView) findViewById (R.id.textview1);
textview.setText (year + "-" + monthOfYear + "-" + dayOfMonth);
/ / Use the following procedure will quit Toast, puzzled.
/ / Toast.makeText (getApplicationContext (), year, Toast.LENGTH_SHORT) show ();.

}
};

@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);

final Calendar objTime = Calendar.getInstance ();
int iYear = objTime.get (Calendar.YEAR);
int iMonth = objTime.get (Calendar.MONTH);
int iDay = objTime.get (Calendar.DAY_OF_MONTH);

/ / New DatePickerDialog (MainActivity.this, DatePickerListener, iYear, iMonth, iDay) show ();.
DatePickerDialog picker = new DatePickerDialog (MainActivity.this, DatePickerListener,
iYear, iMonth, iDay);
picker.setCancelable (true);
picker.setCanceledOnTouchOutside (true);
picker.setButton (DialogInterface.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener () {
@ Override
public void onClick (DialogInterface dialog, int which) {
Log.d ("Picker", "Correct behavior!");
}
});
picker.setButton (DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener () {
@ Override
public void onClick (DialogInterface dialog, int which) {
Log.d ("Picker", "Cancel!");
}
});
picker.show ();


}

@ 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;
}

}


Ha just come back, I see! ! !
Reply:
reference to the 16th floor yiyamzen reply:
Quote: references to the 14th floor pengguohua1988 reply:

@ Override
public void onDateSet (DatePicker view, int year, int monthOfYear,
int dayOfMonth) {

I understand this approach, DatePicker is selected the default value, so whether you're OK or Cancel, year, month, and day will be filled in textview in, because you fill in the contents of this method in the.


If the landlord to solve this problem, please share, thank Ha!

Some people say that the Internet is that no matter the point "OK" or "Cancel" will tune onDateSet methods.

This is a bug 4.1 of
Reply:
Hello Ha, that that iYear Toast is because you are int type, you add (+ "") can be
Then I saw that DatePickerDialog source, the feeling is no problem, then I changed the kinds of ideas tried, found it was so
OnDateSet you that you are actually triggered this "dialog" closed, it was going to change the date of recognition rather than change in your "date control, date time value", so, in fact, there is a question, you shut off the dialog, no matter in what way off (for example: press the "back" button, "or touch the screen to another location," or press the "cancle"), certainly will trigger this event, and will After you set the button's onclick own execution finished execution, so you can only write that which led to "the view layer view" things that onDateSet inside, you use this stuff, although convenient, but too inflexible your own layout casual look, put a datePicker and then write their own two button, it will not know how much useful than this! ! !
Reply:
dataset will definitely callback method. The idea itself of this problem is a problem, sometimes the result is the main
Reply:
toast crash, 19 # positive solution!
Reply:
cited 19th floor u011133213 reply:
Hello Ha, that that iYear Toast is because you are int type, you add (+ "") on can
Then I saw that DatePickerDialog source, the feeling is no problem, then I changed the kinds of ideas tried, found it was so
OnDateSet you that you are actually triggered this "dialog" closed, it was going to change the date of recognition rather than change in your "date control, date time value", so, in fact, there is a question, you shut off the dialog, no matter in what way off (for example: press the "back" button, "or touch the screen to another location," or press the "cancle"), certainly will trigger this event, and will After you set the button's onclick own execution finished execution, so you can only write that which led to "the view layer view" things that onDateSet inside, you use this stuff, although convenient, but too inflexible your own layout casual look, put a datePicker and then write their own two button, it will not know how much useful than this! ! !

Toast of it, thank you very much.

No comments:

Post a Comment