我对android studio比较新,我的对话框有问题。在我的profile_activity中安装应用程序后,我想显示一个对话框。然而,它之后出现在Main Activity中,我相信它与MainActivity中的上下文有关,我将它的上下文传递给静态类。如果有人可以解释为什么会这样,以及是否有办法解决这个问题,那么代码我会非常感激。这是扩展profile_activity的MainActivity:
public class MainActivity extends profile_activity {
private static Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this.getApplicationContext();
//---Setting property for greeting message and styling it a bit---//
TextView textOut;
textOut = (TextView) findViewById(R.id.textView2);
textOut.setTextColor(Color.parseColor("#FFFFFF"));
textOut.setGravity(Gravity.CENTER);
textOut.setTextSize(getResources().getDimension(R.dimen.textsize));
textOut.setTypeface(null, Typeface.BOLD_ITALIC);
textOut.setText("Hello " + editText.getText() + ", pick one of the zodiac symbols below " +
"to find out what the stars are holding for you today!");
//---Initiallize properties for all of the buttons used in the Main Activity Page ---/
ImageButton aquaButton;
aquaButton = (ImageButton) findViewById(R.id.bAquarius);
ImageButton piscesButton;
piscesButton = (ImageButton) findViewById(R.id.bPisces);
ImageButton ariesButton;
ariesButton = (ImageButton) findViewById(R.id.bAries);
ImageButton taurusButton;
taurusButton = (ImageButton) findViewById(R.id.bTaurs);
ImageButton geminiButton;
geminiButton = (ImageButton) findViewById(R.id.bGemini);
ImageButton cancerButton;
cancerButton = (ImageButton) findViewById(R.id.bCancer);
ImageButton leoButton;
leoButton = (ImageButton) findViewById(R.id.bLeo);
ImageButton virgoButton;
virgoButton = (ImageButton) findViewById(R.id.bVirgo);
ImageButton libraButton;
libraButton = (ImageButton) findViewById(R.id.bLibra);
ImageButton scorpioButton;
scorpioButton = (ImageButton) findViewById(R.id.bScorpio);
ImageButton sagittariusButton;
sagittariusButton = (ImageButton) findViewById(R.id.bSagittarius);
ImageButton capriButton;
capriButton = (ImageButton) findViewById(R.id.bCapricorn);
//---Setting actions for each button when pressed---/
aquaButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent aquaActivity = new Intent(MainActivity.this,
aqua_Activity.class);
startActivity(aquaActivity);
}
});
piscesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent piscesActivity = new Intent(MainActivity.this,
pisces_activity.class);
startActivity(piscesActivity);
}
});
ariesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent ariesActivity = new Intent(MainActivity.this,
aries_activity.class);
startActivity(ariesActivity);
}
});
taurusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent taurusActivity = new Intent(MainActivity.this,
taurus_activity.class);
startActivity(taurusActivity);
}
});
geminiButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent geminiActivity = new Intent(MainActivity.this,
gemini_activity.class);
startActivity(geminiActivity);
}
});
cancerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent cancerActivity = new Intent(MainActivity.this,
cancer_activity.class);
startActivity(cancerActivity);
}
});
leoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent leoActivity = new Intent(MainActivity.this,
leo_activity.class);
startActivity(leoActivity);
}
});
virgoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent virgoActivity = new Intent(MainActivity.this,
virgo_activity.class);
startActivity(virgoActivity);
}
});
libraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent libraActivity = new Intent(MainActivity.this,
libra_activity.class);
startActivity(libraActivity);
}
});
scorpioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent scorpioActivity = new Intent(MainActivity.this,
scorpio_activity.class);
startActivity(scorpioActivity);
}
});
sagittariusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent sagittActivity = new Intent(MainActivity.this,
sagittarius_activity.class);
startActivity(sagittActivity);
}
});
capriButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent capriActivity = new Intent(MainActivity.this,
capricorn_activity.class);
startActivity(capriActivity);
}
});
}
//--Initializing static methods for navigation--//
private static void goToCapriAct() {
Intent capriacti = new Intent(mContext, capricorn_activity.class);
mContext.startActivity(capriacti);
}
private static void goToAquaAct() {
Intent aquaacti = new Intent(mContext, aqua_Activity.class);
mContext.startActivity(aquaacti);
}
private static void goToPiscAct() {
Intent piscacti = new Intent(mContext, pisces_activity.class);
mContext.startActivity(piscacti);
}
private static void goToAriesAct() {
Intent ariesacti = new Intent(mContext, aries_activity.class);
mContext.startActivity(ariesacti);
}
private static void goToTaurAct() {
Intent taursacti = new Intent(mContext, taurus_activity.class);
mContext.startActivity(taursacti);
}
private static void goToGeminiAct() {
Intent geminiacti = new Intent(mContext, gemini_activity.class);
mContext.startActivity(geminiacti);
}
private static void goToCancAct() {
Intent cancacti = new Intent(mContext, cancer_activity.class);
mContext.startActivity(cancacti);
}
private static void goToLeoAct() {
Intent leoacti = new Intent(mContext, leo_activity.class);
mContext.startActivity(leoacti);
}
private static void goToVirgoAct() {
Intent virgoacti = new Intent(mContext, virgo_activity.class);
mContext.startActivity(virgoacti);
}
private static void goToLibraAct() {
Intent libraacti = new Intent(mContext, libra_activity.class);
mContext.startActivity(libraacti);
}
private static void goToScorpAct() {
Intent scorpacti = new Intent(mContext, scorpio_activity.class);
mContext.startActivity(scorpacti);
}
private static void goToSaggAct() {
Intent saggacti = new Intent(mContext, sagittarius_activity.class);
mContext.startActivity(saggacti);
}
//--Setting DatePickerFragment--//
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
Calendar myCalendar = Calendar.getInstance();
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
int year = myCalendar.get(Calendar.YEAR);
int month = myCalendar.get(Calendar.MONTH);
int day = myCalendar.get(Calendar.DAY_OF_MONTH);
// Create a new instance o f DatePickerDialog and return it
int theme;
if (Build.VERSION.SDK_INT < 23) theme = AlertDialog.THEME_HOLO_DARK;
else theme = android.R.style.Theme_Holo_Dialog;
return new DatePickerDialog(getActivity(), theme, this, year, month, day);
}
//--Checks about every single sign and navigation--//
String message;
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
month = month + 1;
if ((month == 12 && day >= 22 && day <= 31) || (month == 1 && day >= 1 && day <= 19)){
goToCapriAct();
}
else if ((month == 1 && day >= 20 && day <= 31) || (month == 2 && day >= 1 && day <= 17)){
goToAquaAct();
}
else if ((month == 2 && day >= 18 && day <= 29) || (month == 3 && day >= 1 && day <= 19)) {
goToPiscAct();
}
else if ((month == 3 && day >= 20 && day <= 31) || (month == 4 && day >= 1 && day <= 19)) {
goToAriesAct();
}
else if ((month == 4 && day >= 20 && day <= 30) || (month == 5 && day >= 1 && day <= 20)) {
goToTaurAct();
}
else if ((month == 5 && day >= 21 && day <= 31) || (month == 6 && day >= 1 && day <= 20)) {
goToGeminiAct();
}
else if ((month == 6 && day >= 21 && day <= 30) || (month == 7 && day >= 1 && day <= 22)) {
goToCancAct();
}
else if ((month == 7 && day >= 23 && day <= 31) || (month == 8 && day >= 1 && day <= 22)) {
goToLeoAct();
}
else if ((month == 8 && day >= 23 && day <= 31) || (month == 9 && day >= 1 && day <= 22)) {
goToVirgoAct();
}
else if ((month == 9 && day >= 23 && day <= 30) || (month == 10 && day >= 1 && day <= 22)) {
goToLibraAct();
}
else if ((month == 10 && day >= 23 && day <= 31) || (month == 11 && day >= 1 && day <= 21)) {
goToScorpAct();}
else if ((month == 11 && day >= 22 && day <= 30) || (month == 12 && day >= 1 && day <= 21)) {
goToSaggAct();}
else
{ System.out.println("Illegal date");}
Toast toast = Toast.makeText(getContext(), message, Toast.LENGTH_SHORT);
toast.show();
}
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new MainActivity.DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
public View.OnClickListener exitButtonLitener = new View.OnClickListener() {
public void onClick(View v) {
notificationcall();
finish();
System.exit(0);
}
};
public View.OnClickListener aboutButtonListener = new View.OnClickListener() {
public void onClick(View v) {
Intent about_activity = new Intent(MainActivity.this,
aboutActivity.class);
startActivity(about_activity);
}
};
public void makeTag(String tag) {
String or = savedname.getString(tag, null);
SharedPreferences.Editor preferencesEditor = savedname.edit();
preferencesEditor.putString("tag", tag);
preferencesEditor.apply();
}
public View.OnClickListener saveButtonListener = new View.OnClickListener() {
public void onClick(View v) {
//---Setting requirements for editText property---//
if (editText.getText().length() > 0) {
makeTag(editText.getText().toString());
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(editText.getWindowToken(), 0);
Intent main_activity = new Intent(MainActivity.this,
MainActivity.class);
startActivity(main_activity);
}
}
};}
这是profile_activity:
public class profile_activity extends AppCompatActivity {
//---Setting public proerty for variables inherited in Main Activity---//
private Context cnt;
private String m_Text = "";
public EditText editText;
SharedPreferences savedname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_activity);
cnt = this;
if(isFirstTime()){
AlertDialog.Builder builder = new AlertDialog.Builder(profile_activity.this);
builder.setTitle("Title");
// Set up the input
final EditText input = new EditText(cnt);
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
input.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
builder.setView(input);
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
m_Text = input.getText().toString();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();}
//---Initiallizing the properties used in profile activity---//
editText = (EditText) findViewById(R.id.name);
savedname = getSharedPreferences("notes", MODE_PRIVATE);
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
ImageButton btn2 = (ImageButton) findViewById(R.id.btn2);
ImageButton btn3 = (ImageButton) findViewById(R.id.btn3);
editText.setText(savedname.getString("tag",null));
//---Initiallizing functions for each button--//
btn1.setOnClickListener(saveButtonListener);
btn2.setOnClickListener(aboutButtonListener);
btn3.setOnClickListener(exitButtonLitener);
}
//---Creating public function for notifications---//
public void notificationcall(){
//---Setting parameters to the notification---//
NotificationCompat.Builder notifBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setSmallIcon(R.drawable.notificaion_icon)
.setTicker("Alert New Message")
.setContentTitle("Goodbye!")
.setContentText("It was nice having you today! Come back tomorrow for your new horoscope!")
.setAutoCancel(true);
//---Giving funcionality to the notification---//
Intent resultIntent = new Intent(this, MainActivity.class);
//---Back stack supporting navigation of the notification---//
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
notifBuilder.setContentIntent(resultPendingIntent);
//---Building the application---//
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notifBuilder.build());
}
private boolean isFirstTime()
{
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean ranBefore = preferences.getBoolean("RanBefore", false);
if (!ranBefore) {
// first time
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("RanBefore", true);
editor.apply();
}
return !ranBefore;
}
public View.OnClickListener exitButtonLitener = new View.OnClickListener(){
public void onClick(View v){
notificationcall();
finish();
System.exit(0);
}
};
public View.OnClickListener aboutButtonListener = new View.OnClickListener(){
public void onClick(View v){
Intent about_activity = new Intent (profile_activity.this,
aboutActivity.class);
startActivity(about_activity);
}
};
public void makeTag(String tag){
String or = savedname.getString(tag, null);
SharedPreferences.Editor preferencesEditor = savedname.edit();
preferencesEditor.putString("tag",tag);
preferencesEditor.apply();
}
public View.OnClickListener saveButtonListener = new View.OnClickListener(){
public void onClick(View v) {
//---Setting requirements for editText property---//
if (editText.getText().length()>0){
makeTag(editText.getText().toString());
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(editText.getWindowToken(),0);
Intent main_activity = new Intent(profile_activity.this,
MainActivity.class);
startActivity(main_activity);
}
}
};
答案 0 :(得分:0)
在您MainActivity
方法内的onCreate()
课程中,您super.OnCreate()
致电。因此,当您profile_activity
被调用时,onCreate()
会被执行一次,然后来自MainActivity
来电。这就是为什么你得到2个对话框而不是一次。由于AlertDialog.Builder object.show()
位于profile_activity
onCreate()
内。
因此,如果您从MainActivity super
中移除onCrreate()
来电,则应该可以解决问题。