Android 如何在微调器选择中更改默认选择时显示警报?
我有一个包含6个值的数组。。 我在微调器中设置了该数组,默认选择为3。。 当用户选择下面3时,我想显示一个警报对话框,如无法选择。。如果用户选择上述3项,则表示我希望显示一个警报对话框,如“您确定要更改吗…”。。 问题是在更改了上述3项后,我将微调器的选择设置为更改位置。。但是警报对话框在我的程序中显示了双倍时间。。 请给我什么解决办法Android 如何在微调器选择中更改默认选择时显示警报?,android,spinner,selection,Android,Spinner,Selection,我有一个包含6个值的数组。。 我在微调器中设置了该数组,默认选择为3。。 当用户选择下面3时,我想显示一个警报对话框,如无法选择。。如果用户选择上述3项,则表示我希望显示一个警报对话框,如“您确定要更改吗…”。。 问题是在更改了上述3项后,我将微调器的选择设置为更改位置。。但是警报对话框在我的程序中显示了双倍时间。。 请给我什么解决办法 import android.app.ProgressDialog; import android.content.DialogInterface; impor
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import com.android.c_fiber.Intro_Slider.SessionManagement;
import java.util.Arrays;
import java.util.HashMap;
public class spinnerDoubt extends AppCompatActivity{
String[] plans,months;
int changed_plan_position,changed_month_position;
int plan_position,plan_month;
String selectedItem;
Spinner spinner_plan,spinner_month;
String plan;
String month = "1";
boolean isChanged = false;
boolean isMonthChanged = false;
SessionManagement sessionManagement;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay_details);
plans = new String[]{"Start","Fast","Hot","Pace","Electric","Thunder"};
months = new String[]{"1","3","6","12"};
spinner_plan = (Spinner) findViewById(R.id.pay_plan);
spinner_month = (Spinner) findViewById(R.id.pay_month);
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();
Runnable progressRunnable = new Runnable() {
@Override
public void run() {
sessionManagement = new SessionManagement(this);
HashMap<String,String> hashMap = sessionManagement.getUserDetails();
plan = hashMap.get(SessionManagement.KEY_PLAN_NAME);
plan_position = Arrays.asList(plans).indexOf(plan);
plan_month = Arrays.asList(months).indexOf(month);
ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.layout_spinner,R.id.spinner_text,plans);
spinner_plan.setAdapter(arrayAdapter);
spinner_plan.setSelection(plan_position);
ArrayAdapter arrayAdapter_month = new ArrayAdapter(this,R.layout.layout_spinner,R.id.spinner_text,months);
spinner_month.setAdapter(arrayAdapter_month);
spinner_month.setSelection(plan_month);
progressDialog.dismiss();
}
};
Handler pdCanceller = new Handler();
pdCanceller.postDelayed(progressRunnable, 2000);
spinner_plan.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
selectedItem = parent.getItemAtPosition(position).toString();
int selectPosition = parent.getSelectedItemPosition();
Log.e("plan_position_1",plan_position+"");
Log.e("selectedItem",selectPosition+"");
if (!isChanged)
{
if (selectPosition > plan_position)
{
dialogInterfacePlan("Are You Sure To Change!..");
}
else if (selectPosition < plan_position)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("You Can't Downgrade.. \n Please Contact Our Customer Care Executive.. \n Thank You..")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
spinner_plan.setSelection(plan_position);
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
else {
if (selectPosition == plan_position)
{
isChanged = true;
dialogInterfacePlan("Are You Sure To Change!..");
}
else if (selectPosition>plan_position)
{
isChanged = true;
dialogInterfacePlan("Are You Sure To Change!..");
}
else if (selectPosition<plan_position) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("You Can't Downgrade.. \n Please Contact Our Customer Care Executive.. \n Thank You..")
.setCancelable(false)
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
isChanged = true;
spinner_plan.setSelection(changed_plan_position);
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});
spinner_month.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
int selectPosition = parent.getSelectedItemPosition();
if (!isMonthChanged)
{
if (selectPosition>plan_month)
{
dialogInterfaceMonth("Are You Sure To Change?..");
}
}
else
{
if (selectPosition == plan_month){
isChanged = true;
dialogInterfaceMonth("Are You Sure To Change?..");
}
else if (selectPosition > changed_month_position){
isChanged = true;
dialogInterfaceMonth("Are You Sure To Change?..");
}
else if (selectPosition<changed_month_position) {
isChanged = true;
dialogInterfaceMonth("Are You Sure To Change?..");
}
}
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});
}
public void dialogInterfacePlan(String text)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(text)
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
changed_plan_position = Arrays.asList(plans).indexOf(selectedItem);
isChanged = true;
Log.e("After Changed",plan);
Log.e("After Changed",changed_plan_position+"");
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (isChanged) {
spinner_plan.setSelection(changed_plan_position);
dialogInterface.cancel();
}
else
{
spinner_plan.setSelection(plan_position);
dialogInterface.cancel();
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void dialogInterfaceMonth(String text)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(text)
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String change_month = spinner_month.getSelectedItem().toString();
changed_month_position = Arrays.asList(months).indexOf(change_month);
isMonthChanged = true;
if (changed_month_position == 0)
{
spinner_month.setSelection(changed_month_position);
}
else if (changed_month_position == 1)
{
spinner_month.setSelection(changed_month_position);
}
else if (changed_month_position == 2)
{
spinner_month.setSelection(changed_month_position);
}
else
{
spinner_month.setSelection(changed_month_position);
}
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (isChanged) {
spinner_month.setSelection(changed_month_position);
dialogInterface.cancel();
}
else
{
spinner_month.setSelection(plan_month);
dialogInterface.cancel();
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
导入android.app.ProgressDialog;
导入android.content.DialogInterface;
导入android.os.Handler;
导入android.support.v7.app.AlertDialog;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.Spinner;
导入com.android.c_fiber.Intro_Slider.SessionManagement;
导入java.util.array;
导入java.util.HashMap;
公共类喷丝头活动{
字符串[]计划,月;
int更改计划位置、更改月份位置;
int计划职位,计划月份;
字符串selectedItem;
微调器微调器_计划,微调器_月;
串平面图;
字符串month=“1”;
布尔值isChanged=false;
布尔值=false;
会话管理会话管理;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u pay\u details);
计划=新字符串[]{“开始”、“快速”、“热”、“速度”、“电气”、“雷电”};
月份=新字符串[]{“1”、“3”、“6”、“12”};
微调器计划=(微调器)findViewById(R.id.pay\u计划);
微调器月=(微调器)findViewById(R.id.pay\u月);
final ProgressDialog ProgressDialog=新建ProgressDialog(此);
progressDialog.setMessage(“加载…”);
progressDialog.setProgressStyle(progressDialog.STYLE_微调器);
progressDialog.show();
Runnable progressRunnable=new Runnable(){
@凌驾
公开募捐{
sessionManagement=新的sessionManagement(this);
HashMap HashMap=sessionManagement.getUserDetails();
plan=hashMap.get(SessionManagement.KEY\u plan\u NAME);
plan_position=数组.asList(plans).indexOf(plan);
plan_month=Arrays.asList(月).indexOf(月);
ArrayAdapter ArrayAdapter=新的ArrayAdapter(这个,R.layout.layout\u微调器,R.id.spinner\u文本,平面);
微调器\平面设置适配器(阵列适配器);
微调器\平面设置选择(平面位置);
ArrayAdapter ArrayAdapter\u month=新的ArrayAdapter(this,R.layout.layout\u微调器,R.id.spinner\u text,months);
spinner_month.setAdapter(arrayAdapter_month);
微调器月设置选择(计划月);
progressDialog.disclose();
}
};
Handler pdCanceller=新处理程序();
pCanceller.postDelayed(progressRunnable,2000年);
微调器\u plan.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener()
{
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id)
{
selectedItem=parent.getItemAtPosition(position.toString();
int-selectPosition=parent.getSelectedItemPosition();
Log.e(“平面位置1”,平面位置+”);
Log.e(“selectedItem”,selectPosition+”);
如果(!ischange)
{
如果(选择位置>计划位置)
{
dialogInterfacePlan(“您确定要更改吗!”);
}
else if(选择位置<计划位置)
{
AlertDialog.Builder=新建AlertDialog.Builder(此);
builder.setMessage(“您不能降级..\n请联系我们的客户服务主管..\n谢谢..”)
.setCancelable(错误)
.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
微调器\平面设置选择(平面位置);
dialog.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}
}
否则{
如果(选择位置==计划位置)
{
isChanged=true;
dialogInterfacePlan(“您确定要更改吗!”);
}
否则如果(选择位置>计划位置)
{
isChanged=true;
dialogInterfacePlan(“您确定要更改吗!”);
}
else if(选择位置父项、视图、整数位置、长id)
{
int-selectPosition=parent.getSelectedItemPosition();
如果(!isMonthChanged)
{
如果(选择职位>计划月)
{
dialogInterfaceMonth(“您确定要更改吗?”);
}
}
其他的
{
如果(选择位置==计划月){
isChanged=true;
dialogInterfaceMonth(“您确定要更改吗?”);
}
否则如果(选择位置>更改的月份位置){
isChanged=true;
dialogInterfaceMonth(“你确定
spinner_plan.setSelection(plan_position);
boolean isChanged = false;
boolean isMonthChanged = false;
boolean shouldShowDialog=true;
.......
spinner_plan.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedItem = parent.getItemAtPosition(position).toString();
final int selectPosition = parent.getSelectedItemPosition();
Log.e("plan_position_1", plan_position + "");
Log.e("selectedItem", selectPosition + "");
if(shouldShowDialog){
if (!isChanged) {
if (selectPosition > plan_position) {
dialogInterfacePlan("Are You Sure To Change!..");
} else if (selectPosition < plan_position) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("You Can't Downgrade.. \n Please Contact Our Customer Care Executive.. \n Thank You..")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
spinner_plan.setSelection(plan_position);
shouldShowDialog = false;
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
} else {
if (selectPosition == plan_position) {
isChanged = true;
dialogInterfacePlan("Are You Sure To Change!..");
} else if (selectPosition > plan_position) {
isChanged = true;
dialogInterfacePlan("Are You Sure To Change!..");
} else if (selectPosition < plan_position) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("You Can't Downgrade.. \n Please Contact Our Customer Care Executive.. \n Thank You..")
.setCancelable(false)
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
isChanged = true;
spinner_plan.setSelection(changed_plan_position);
shouldShowDialog = false;
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
}
shouldShowDialog = true;
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent) {
}
});
public void dialogInterfacePlan(String text) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(text)
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
changed_plan_position = Arrays.asList(plans).indexOf(selectedItem);
isChanged = true;
Log.e("After Changed", plan);
Log.e("After Changed", changed_plan_position + "");
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (isChanged) {
spinner_plan.setSelection(changed_plan_position);
shouldShowDialog=false;
dialogInterface.cancel();
} else {
spinner_plan.setSelection(plan_position);
dialogInterface.cancel();
}
}
});
AlertDialog alert = builder.create();
alert.show();
}