【问题标题】:How to remove OnClickListener method & directly show the result in TextView如何删除 OnClickListener 方法并直接在 TextView 中显示结果
【发布时间】:2017-04-06 11:31:53
【问题描述】:

我已经构建了一个应用程序,当我们单击 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);
}

}

【问题讨论】:

  • 请大家帮帮我
  • 问题不清楚
  • 如果您的逻辑工作正常,只需从 oncreate 调用它?然后它会在您的应用启动时启动
  • 我已经选择了两个日期选择器,我正在减去两个日期。当我们单击它时,结果会显示在 no_of_days 文本视图中。我想在点击第二个日期选择器后自动显示结果
  • 所以你想在日期选择器上有一个 OnclickEvent 吗?然后你只需要调用逻辑,它就会更新文本视图

标签: android android-studio textview onclicklistener


【解决方案1】:

在日期选择器函数中输入以下代码

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-15
    • 1970-01-01
    • 2020-12-13
    • 2016-05-14
    • 2015-10-15
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多