如何重用代码

时间:2016-11-23 14:35:48

标签: android

所以这是我的提醒应用程序屏幕截图,因为你可以看到它有一个+按钮和一个按钮。当+按钮被cliked时,将显示一个带有editText的对话框。

enter image description here

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add_task:
                mClickedPosition = -1;
                final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                LayoutInflater inflater = LayoutInflater.from(getActivity());
                View promptView = getActivity().getLayoutInflater().inflate(R.layout.dialog_with_edittext, null);
                Button cancel = (Button) promptView.findViewById(R.id.cancelBtn);
                Button save = (Button) promptView.findViewById(R.id.okBtn);
                task = (EditText) promptView.findViewById(R.id.task);
                time = (EditText) promptView.findViewById(R.id.time);
                date = (EditText) promptView.findViewById(R.id.date);
                final AlertDialog alert = builder.create();
                date.setOnClickListener(this);

                time.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        Fragment fragDialog = getActivity().getSupportFragmentManager().findFragmentByTag("TimePicker");
                        if (fragDialog == null) { // Fragment not added
                            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
                            tp.show(ft, "TimePicker");
                        } else {
                            // already active
                        }
                    }
                });

                cancel.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        alert.dismiss();
                    }
                });

                save.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        String addTask = task.getText().toString();
                        String time1 = time.getText().toString();
                        String date1 = date.getText().toString();
                        if ((time1.trim().equals(""))|| (date1.trim().equals("") )|| (addTask.trim().equals(""))) {
                            Toast.makeText(getActivity(), " Not Completed", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            if (adapter != null) {
                                if (mClickedPosition == -1) {
                                    adapter.add(iD, addTask, time1, date1);
                                    insertTask(addTask, time1, date1);
                                    listview.setAdapter(adapter);
                                    alert.dismiss();
                                    check();
                                    retrieveList(name);
                                }
                            }

                            Calendar alarm = Calendar.getInstance();
                            Calendar copy = (Calendar) alarm.clone();
                            alarm.set(Calendar.YEAR, year1);
                            alarm.set(Calendar.MONTH, month1);
                            alarm.set(Calendar.DAY_OF_MONTH, day1);
                            alarm.set(Calendar.HOUR_OF_DAY, hour1);
                            alarm.set(Calendar.MINUTE, min1);
                            int result = alarm.compareTo(copy);

                            if(result < 0) {
                                alarm.add(Calendar.DATE, 1);
                            }
                            else
                            {
                                Intent myIntent = new Intent(getActivity(), MyReceiver.class);
                                pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);
                                AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(getActivity().ALARM_SERVICE);
                                alarmManager.set(AlarmManager.RTC, alarm.getTimeInMillis(), pendingIntent);
                            }
                        }
                    }
                });

                alert.setView(promptView);
                alert.show();
                return true;

如果单击该按钮,它将打开与+按钮完全相同的对话框。我现在的问题是,有没有办法可以重复使用代码,因此代码看起来会更短,或者我必须复制代码并粘贴到按钮onClick

显示我的活动完整代码。如您所见,大多数代码都是相同的(+按钮,按钮以及listView单击)

http://pastebin.com/HEKcmVK3

2 个答案:

答案 0 :(得分:0)

您需要通过创建自定义对话框类并编写视图方法来完成此操作。 然后在+和按钮单击中,创建该类的实例并显示它。

答案 1 :(得分:0)

您可以创建这部分代码的方法,然后只需调用onclicklistener中的方法

String addTask = task.getText().toString();
String time1 = time.getText().toString();
String date1 = date.getText().toString();
if ((time1.trim().equals(""))|| (date1.trim().equals("") )|| (addTask.trim().equals(""))) {
    Toast.makeText(getActivity(), " Not Completed", Toast.LENGTH_SHORT).show();
}
else {
    if (adapter != null) {
        if (mClickedPosition == -1) {
            adapter.add(iD, addTask, time1, date1);
            insertTask(addTask, time1, date1);
            listview.setAdapter(adapter);
            alert.dismiss();
            check();
            retrieveList(name);
        }
    }

    Calendar alarm = Calendar.getInstance();
    Calendar copy = (Calendar) alarm.clone();
    alarm.set(Calendar.YEAR, year1);
    alarm.set(Calendar.MONTH, month1);
    alarm.set(Calendar.DAY_OF_MONTH, day1);
    alarm.set(Calendar.HOUR_OF_DAY, hour1);
    alarm.set(Calendar.MINUTE, min1);
    int result = alarm.compareTo(copy);

    if(result < 0) {
        alarm.add(Calendar.DATE, 1);
    }
    else
    {
        Intent myIntent = new Intent(getActivity(), MyReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);
        AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(getActivity().ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC, alarm.getTimeInMillis(), pendingIntent);
    }
}