如何删除OnClickListener方法&直接在TextView中显示结果

时间:2017-04-06 11:31:54

标签: android android-studio textview onclicklistener

我已经构建了一个应用程序,当我点击TextView时,我会在TextView中获得天数差异。我通过搜索谷歌这样做了。但现在我希望结果应该自动显示在TextView中。我已将TextView的ID设为自动显示结果,ID为 no_of_days

我的代码是:

public class Leave extends AppCompatActivity {

TextView date;
private DatePickerDialog datePickerDialog;
//TextView date2;
//TextView setDay;
private DatePickerDialog datePickerDialog2;
TextView no_of_days;
String date1 = "", date2 = "";
private TextView strdate2;
private TextView strdate;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_leave);
     strdate = (TextView) findViewById(R.id.date);
    strdate2 = (TextView) findViewById(R.id.date2);
    //setDay = (TextView) findViewById(R.id.setDay);
    no_of_days = (TextView) findViewById(R.id.no_of_days);
    final RadioButton radio_full = (RadioButton) findViewById(R.id.radio_full);
    final RadioButton radio_Half = (RadioButton) findViewById(R.id.radio_Half);
    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioleave);
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            int buttonId = radioGroup.getCheckedRadioButtonId();
            switch (buttonId) {
                case R.id.radio_full:
                    Toast.makeText(getApplicationContext(), "You have selected Full Day Leave", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.radio_Half:
                    Toast.makeText(getApplicationContext(), "You have selected Half Day Leave", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    });

    // initially hide the radio buttons
    radio_full.setVisibility(View.GONE);
    radio_Half.setVisibility(View.GONE);

    // initiate the date picker and a button
    strdate= (TextView) findViewById(R.id.date);
    // perform click event on edit text
    strdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // calender class's instance and get current date , month and year from calender
            final Calendar c = Calendar.getInstance();
            int mYear = c.get(Calendar.YEAR); // current year
            int mMonth = c.get(Calendar.MONTH); // current month
            int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
            // date picker dialog
            datePickerDialog = new DatePickerDialog(Leave.this,
                    new DatePickerDialog.OnDateSetListener() {

                        @Override
                        public void onDateSet(DatePicker view, int year,
                                              int monthOfYear, int dayOfMonth) {
                            // set day of month , month and year value in the edit text
                            // assign the value to date1
                            date1 = dayOfMonth + "/"
                                    + (monthOfYear + 1) + "/" + year;

                            strdate.setText(dayOfMonth + "/"
                                    + (monthOfYear + 1) + "/" + year);

                            // check if date2 has been set and compare it with date1
                            if (!TextUtils.isEmpty(date2)) {
                                // get difference
                                getDifferenceDays(date, date2 );

                                if(date1.equals(date2)) {
                                    // pop up radio button
                                    radio_full.setVisibility(View.VISIBLE);
                                    radio_Half.setVisibility(View.VISIBLE);
                                } else {
                                    // hide radio buttons
                                    radio_full.setVisibility(View.GONE);
                                    radio_Half.setVisibility(View.GONE);
                                }
                            }

                        }
                    }, mYear, mMonth, mDay);
            datePickerDialog.show();
        }
    });

    // initiate the date picker and a button
   strdate2 = (TextView) findViewById(R.id.date2);
    // perform click event on edit text
    strdate2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // calender class's instance and get current date , month and year from calender
            final Calendar c = Calendar.getInstance();
            int mYear = c.get(Calendar.YEAR); // current year
            int mMonth = c.get(Calendar.MONTH); // current month
            int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
            // date picker dialog
            datePickerDialog2 = new DatePickerDialog(Leave.this,
                    new DatePickerDialog.OnDateSetListener() {

                        @Override
                        public void onDateSet(DatePicker view, int year,
                                              int monthOfYear, int dayOfMonth) {
                            // set day of month , month and year value in the edit text

                            // assign the value to date2
                            date2 = dayOfMonth + "/"
                                    + (monthOfYear + 1) + "/" + year;

                            strdate2.setText(dayOfMonth + "/"
                                    + (monthOfYear + 1) + "/" + year);
                            // check if date2 has been set and compare it with date1
                            if (!TextUtils.isEmpty(date1)) {
                                // get difference
                                getDifferenceDays(date, date2);

                                if(date1.equals(date2)) {
                                    // pop up radio button
                                    radio_full.setVisibility(View.VISIBLE);
                                    radio_Half.setVisibility(View.VISIBLE);
                                } else {
                                    // hide radio buttons
                                    radio_full.setVisibility(View.GONE);
                                    radio_Half.setVisibility(View.GONE);
                                }
                            }

                        }
                    }, mYear, mMonth, mDay);
            datePickerDialog2.show();

        }
    });

    no_of_days.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
            String date01 = strdate.getText().toString();
            String date02 = strdate2.getText().toString();
            try {
                Date d = format.parse(date01);
                Date d1 = format.parse(date02);
                getDifferenceDays(d, d1);
            } catch (ParseException e) {
                e.printStackTrace();
            }


        }
    });


}


private void getDifferenceDays(TextView d, String d1) {

}

public void getDifferenceDays(Date d1, Date d2) {
    int daysdiff = 0;
    long diff = d2.getTime() - d1.getTime();
    long diffDays = diff / (24 * 60 * 60 * 1000) + 1;
    daysdiff = (int) diffDays;
    no_of_days.setText(Integer.toString(daysdiff));
    System.out.println("day count=>" + daysdiff);
}

}

1 个答案:

答案 0 :(得分:0)

在日期选择器功能

中使用以下代码
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");

            try {
                Date d = format.parse(date1);
                Date d1 = format.parse(date2);
                getDifferenceDays(d, d1);
            } catch (ParseException e) {
                e.printStackTrace();
            }

而不是调用此方法getDifferenceDays(date,date2);