一尘不染

Android:如何使用单个按钮执行多个任务

java

我有1个活动按钮。我想将此1按钮用于多个任务。

那我该怎么办?

  • 如果我第一次按此按钮,则更改为2按钮

  • 如果我按了第二次,那么它将更新我的数据

但是第一次只能工作第二次不能工作

看看我的代码我尝试了什么

Intent extras = getIntent();
        {
            if (extras.hasExtra("edit")) {
                if (extras.getStringExtra("edit").equals("home")) {

                    etCompanyName.setEnabled(false);
                    etWebsite.setEnabled(false);
                    etEmail.setEnabled(false);
                    etPhoneHome.setEnabled(false);
                    etPhonePrimary.setEnabled(false);
                    etAddressLine1.setEnabled(false);
                    etAddressLine2.setEnabled(false);
                    etCity.setEnabled(false);
                    spStates.setEnabled(false);
                    etZip.setEnabled(false);
                    spContries.setEnabled(false);
                    //1st time use hear 
              txtSave.setText(getResources().getString(R.string.label_edit));
                    txtClose.setText(getResources().getString(R.string.label_back));
                    txtSave.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                    txtSave.setText(getResources().getString(R.string.label_add));
                            txtClose.setText(getResources().getString(R.string.label_cancel));

                            etCompanyName.setEnabled(true);
                            etWebsite.setEnabled(true);
                            etEmail.setEnabled(true);
                            etPhoneHome.setEnabled(true);
                            etPhonePrimary.setEnabled(true);
                            etAddressLine1.setEnabled(true);
                            etAddressLine2.setEnabled(true);
                            etCity.setEnabled(true);
                            spStates.setEnabled(true);
                            etZip.setEnabled(true);
                            spContries.setEnabled(true);
                        }
                    });
                    if (extras != null) {
                        Company value = (Company) extras.getSerializableExtra("company");
                        etCompanyName.setText(value.getName());
                        etWebsite.setText(value.getWebsite());
                        etEmail.setText(value.getEmail());
                        etPhoneHome.setText(value.getPhoneHome());
                        etPhonePrimary.setText(value.getPhonePrimary());
                        etAddressLine1.setText(value.getAddressLine1());
                        etAddressLine2.setText(value.getAddressLine2());
                        etCity.setText(value.getCity());
                        etZip.setText(value.getZipcode());
                    }
                } else {
                    //2nd time use hear 
                    txtSave.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Company company = new Company();
                            company.setName(etCompanyName.getText().toString().trim());
                            company.setWebsite(etWebsite.getText().toString().trim());
                            company.setEmail(etEmail.getText().toString().trim());
                            company.setPhoneHome(etPhoneHome.getText().toString().trim());
                            company.setPhonePrimary(etPhonePrimary.getText().toString().trim());
                            company.setAddressLine1(etAddressLine1.getText().toString().trim());
                            company.setAddressLine2(etAddressLine2.getText().toString().trim());
                            company.setZipcode(etZip.getText().toString().trim());
                            company.setCity(etCity.getText().toString().trim());
                            company.setState(spStates.getSelectedItem().toString());
                            company.setCountry(spContries.getSelectedItem().toString());
                            company.setDate(Util.getInstance(AddCompanyActivity.this).getCurrentDate());

                            long isUpdated = myDb.updateCompany(company);

                            if (isUpdated != -1) {
                                Toast.makeText(getApplicationContext(), "Company Update Successfully: " + isUpdated, Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(getApplicationContext(), "Something wrong", Toast.LENGTH_SHORT).show();
                            }
                            finish();
                        }
                    });
                }
            }
        }

您可以看到我的上面的代码,我可以使用txtSave按钮执行2个任务,但是只更改两个按钮,我将更改数据并单击按钮,然后它就无法执行


阅读 124

收藏
2020-12-03

共1个答案

一尘不染

尝试这种方式,首先在活动类文件上声明全局变量,如下所示:

int count = 0;

之后,添加您的点击监听器,如下所示:

yourButton.setOnClickListener(v -> {
            if (count == 0) { // the first click
                count++;
                // do your stuff

            }else { // the second click
                count = 0; // initialize the count to limit the button click just for the first and the second time only
                // do your stuff
            }

        });
2020-12-03