我正在研究我的Spinner。当我选择一个项目并单击SEND按钮时,我想要在打开新活动之前首先显示一个警告对话框(仅限OK按钮)。寻求帮助。谢谢。 :')
无论如何,这是我的源代码。 :') MainActivity.java
package com.example.imelda.fortesting;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class MainActivity extends ActionBarActivity {
private static Button button_send;
private static Button button_sched;
ImageView btn_Animate;
Spinner spin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_spinner_dropdown_item,
getResources().getStringArray(R.array.punpColleges));
spin.setAdapter(spinnerAdapter);
OnClickButtonListener();
OnClickButtonSchedule();
}
public void OnClickButtonListener() {
button_send = (Button) findViewById(R.id.button);
button_send.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if (spin.getSelectedItem().toString().equals("CCS")) {
Intent intent = new Intent("com.example.imelda.mythesis.ListActivity");
startActivity(intent);
} else {
startActivity(new Intent("com.example.imelda.mythesis.SecondTesting"));
}
}
}
);
}
答案 0 :(得分:0)
关闭对话框时启动新的Activity。
new AlertDialog.Builder(this)
.setTitle("hi")
.setMessage("Hello world")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
//open activity
}
}).create().show();
答案 1 :(得分:0)
[self.delegate metaDataChanged:self]
答案 2 :(得分:0)
您可以将AlertDialog用于此
public void OnClickButtonListener() {
button_send = (Button) findViewById(R.id.button);
button_send.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if (spin.getSelectedItem().toString().equals("CCS")) {
new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Message")
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent("com.example.imelda.mythesis.ListActivity");
startActivity(intent);
}
}).create().show();
} else {
new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Message")
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent("com.example.imelda.mythesis.SecondTesting");
startActivity(intent);
}
}).create().show();
}
}
}
);
}
答案 3 :(得分:0)
在onClick
功能中。
AlertDialog.Builder alertDialogue = new AlertDialog.Builder(getActivity());
alertDialogue.setTitle("Are you sure?");
alertDialogue.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (spin.getSelectedItem().toString().equals("CCS")) {
Intent intent = new Intent("com.example.imelda.mythesis.ListActivity");
startActivity(intent);
} else {
startActivity(new Intent("com.example.imelda.mythesis.SecondTesting"));
}
}
});
alertDialogue.show();
您可以使用MainActivity.java
package com.example.imelda.fortesting;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class MainActivity extends ActionBarActivity {
private static Button button_send;
private static Button button_sched;
ImageView btn_Animate;
Spinner spin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_spinner_dropdown_item,
getResources().getStringArray(R.array.punpColleges));
spin.setAdapter(spinnerAdapter);
OnClickButtonListener();
OnClickButtonSchedule();
}
public void OnClickButtonListener() {
button_send = (Button) findViewById(R.id.button);
button_send.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialogue = new AlertDialog.Builder(getActivity());
alertDialogue.setTitle("Are you sure?");
alertDialogue.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (spin.getSelectedItem().toString().equals("CCS")) {
Intent intent = new Intent("com.example.imelda.mythesis.ListActivity");
startActivity(intent);
} else {
startActivity(new Intent("com.example.imelda.mythesis.SecondTesting"));
}
}
});
alertDialogue.show();
}
});
}