【问题标题】:Alert Dialog Box - how to?警报对话框 - 如何?
【发布时间】:2018-08-27 23:11:10
【问题描述】:

在我的项目中。我正在使用两个警报对话框。私有方法中使用的两个警报对话框。私有方法是checkIn( )checkOut( )

在主要活动中,我有一个按钮。按钮的两个功能。一个功能是签入,另一个功能是签出。

当我进入 Activity 时,按钮在 Check-in 中可见。如果我单击按钮。它变为 Check out 并显示 Alert Dialogue checkIn( ) 私有方法。

如果我刷新 Activity 它将变为签入。 当我刷新活动时,我的问题是如何获得结帐操作。任何人都可以解决并为此提供解决方案...

提前谢谢你。我附上了下面的示例代码

public class Insert_DataSql extends AppCompatActivity {


    private long UPDATE_INTERVAL = 2 * 1000;  /* 10 secs */
    private long FASTEST_INTERVAL = 2000; /* 2 sec */
    LocationRequest locationRequest;
    LocationManager locationManager;
    PreparedStatement preparedStatement;
    String formattedDate;
    WebConnection connectionClass;
    Button cin,cout;
    TextView dat,tim,adddre;
    Boolean flag=true;


    SharedPreferences sharedPreferences;
    String username;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insertdata);
        connectionClass = new WebConnection();

        sharedPreferences=getSharedPreferences("LoginPref", Context.MODE_PRIVATE);//here we go getdata
        user name=sharedPreferences.getString("User name",null);

//Button Click able

cin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    buildAlertMessageNoGps();

                } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {


                    if (flag) {
                        getMyCurrentLocation();
                        CheckinButton();
                        flag =false;
                        Toast.makeText(Insert_DataSql.this, "True statement", Toast.LENGTH_SHORT).show();
                        dat.setText(date);
                        new Insert_data().execute("");//insert for button
                        //   tim.setText(formattedDate);

                    }
                    else{
                        flag =true;
                        getMyCurrentLocation();
                        CheckoutButton();
                        Toast.makeText(Insert_DataSql.this, "Wrong statement", Toast.LENGTH_SHORT).show();
                        dat.setText(date);
                        new Insert_data().execute("");
                        // tim.setText(formattedDate);
                    }
                                    }
            }

//警报对话框的私有方法

      private void CheckoutButton() {
        if(flag == false ){
            cin .setText("Check Out Sucessfull");
            AlertDialog alertDialog = new AlertDialog.Builder(
                    Insert_DataSql.this).create();
            alertDialog.setTitle("Check Out Sucessfully");
            alertDialog.setMessage(adddre.getText());
            alertDialog.setIcon(R.drawable.ic_access_time_black_24dp);
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
                }
            });
            alertDialog.show();
        }

    }

    private void CheckinButton() {
        if(flag == true ){
            cin.setText("Check Out");
            AlertDialog alertDialog = new AlertDialog.Builder(
                    Insert_DataSql.this).create();
            alertDialog.setTitle("Check In Sucessfully");
            alertDialog.setMessage("Have A Nice Day");
            alertDialog.setIcon(R.drawable.ic_access_time_black_24dp);
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
                }
            });
            alertDialog.show();
        }
    }

【问题讨论】:

    标签: android android-layout android-fragments android-asynctask


    【解决方案1】:

    试试这样的:

    定义这些变量:

        public static final String PREF_FLAG = "FLAG";
        private static String COUT = "cout";
        private static String CIN = "cin";
        private static String defaultFlagValue = COUT;
    

    并添加这些方法:

        private String getFlag() {
            return sharedPreferences.getString(PREF_FLAG, defaultFlagValue);
        }
    
        private void setFlag(String flag) {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(PREF_FLAG, flag);
            editor.apply();
        }
    

    并更新您的代码:

    private void CheckinButton() {
            if (getFlag() == COUT) {
    
                ...
    
                setFlag(CIN);
            }
    
        }
    
    private void CheckoutButton() {
            if (getFlag() == CIN) {
    
                ...
    
                setFlag(COUT);
            }
        }
    

    最后更新游览onClick函数

    cin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
                if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    buildAlertMessageNoGps();
    
                } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    
    
                    if (getFlag() == CIN) {
                        getMyCurrentLocation();
                        CheckinButton();
                        Toast.makeText(Insert_DataSql.this, "True statement", Toast.LENGTH_SHORT).show();
                        dat.setText(date);
                        new Insert_data().execute("");//insert for button
                        //   tim.setText(formattedDate);
    
                    }
                    else{
    
                        getMyCurrentLocation();
                        CheckoutButton();
                        Toast.makeText(Insert_DataSql.this, "Wrong statement", Toast.LENGTH_SHORT).show();
                        dat.setText(date);
                        new Insert_data().execute("");
                        // tim.setText(formattedDate);
                    }
                }
            }
    }
    

    希望对你有帮助

    【讨论】:

    • 非常感谢先生。我会检查的。
    • 此代码只运行一次。如果我刷新活动它不起作用
    猜你喜欢
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 2023-03-25
    相关资源
    最近更新 更多