代码之家  ›  专栏  ›  技术社区  ›  Leace

单按钮多动作顺序

  •  1
  • Leace  · 技术社区  · 6 年前

    我用一个按钮来完成两个任务。

    1.日期选择 2.两个日期之间的差异

    如:

    我用“onclick”方法来处理两个按钮。

    1. btndate2用于选择日期,onclick为(setdate2)
    2. 按钮9用于计算两个日期之间的差异和onclick is(diff)

    待:

    一个按钮,用于选择日期并计算两个日期之间的差异。它应按顺序排列。例如: 1:选择日期 2。计算日期之间的差异

    当前代码: mainactivity.java(主活动.java)

    package com.bar.example.myapplication;
    
    import android.app.DialogFragment;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.text.format.DateFormat;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;
    import org.threeten.bp.LocalDate;
    import org.threeten.bp.format.DateTimeFormatter;
    import org.threeten.bp.format.DateTimeParseException;
    import org.threeten.bp.temporal.ChronoUnit;
    import java.text.SimpleDateFormat;
    
    public class MainActivity extends AppCompatActivity {
    
    
      public TextView txtResult, tv, textDivNumber, textAVG, txtZaMisiac;
      public static TextView tvresult;
      public Button reset, button, button1, button2, button9, editTextDate3, editTextDate5, btnok;
      public EditText barcodeResult;
      public static EditText courseTitleEditText;
      private ListView offeringsListView;
    
      private static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("d.M.uuuu");
    
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_main);
        editTextDate5 = (Button) findViewById(R.id.editText5);
        button2 = (Button) findViewById(R.id.btnDate2);
        txtResult = (TextView) findViewById(R.id.editText2);
        editTextDate5.setText(DateFormat.format("dd.MM.yyyy", new java.util.Date()).toString());
      }
      public void diff(View view) {
        SimpleDateFormat myFormat = new SimpleDateFormat("dd.MM.yyyy");
        CharSequence inputString1 = editTextDate5.getText();
        try {
          LocalDate date1 = LocalDate.parse(inputString1, dateFormatter);
          CharSequence inputString2 = button2.getText();
          try {
            LocalDate date2 = LocalDate.parse(inputString2, dateFormatter);
            long diffDate = ChronoUnit.DAYS.between(date1, date2);
            txtResult.setText(String.valueOf(diffDate));
          } catch (DateTimeParseException dtpe) {
            Toast.makeText(this, "Date2 is not a valid date: " + inputString2, Toast.LENGTH_SHORT).show();
          }
        } catch (DateTimeParseException dtpe) {
          Toast.makeText(this, "Date1 is not a valid date: " + inputString1, Toast.LENGTH_SHORT).show();
        }
      }
    
      public void setDate1(View view) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "Date Picker");
      }
      public void setDate2(View view) {
        DialogFragment newFragment = new DatePickerFragment2();
        newFragment.show(getFragmentManager(), "Date Picker");
      }
    
      @Override
      public void onPointerCaptureChanged(boolean hasCapture) {
    
      }
    }
    

    日期选择性能2.java

    package com.bar.example.myapplication;
    import android.app.DatePickerDialog;
    import android.app.Dialog;
    import android.app.DialogFragment;
    import android.os.Bundle;
    import android.widget.Button;
    import android.widget.DatePicker;
    import java.util.Calendar;
    
    public class DatePickerFragment2 extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
    
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), this, year, month, day);
      }
    
      public void onDateSet(DatePicker view, int year, int month, int day) {
        Button btnDate2 = (Button) getActivity().findViewById(R.id.btnDate2);
        String stringOfDate = day + "." + (month + 1) + "." + year;
        btnDate2.setText(stringOfDate);
      }
    }
    

    活动\main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">
    
      <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="1">
    
        <TextView android:id="@+id/editText1" android:layout_width="47dp" android:layout_height="wrap_content" android:ems="10" android:inputType="textNoSuggestions" android:text="Date:" />
    
        <Button android:id="@+id/editText5" android:layout_width="47dp" android:layout_height="wrap_content" android:layout_weight="0.50" android:ems="10" android:inputType="textNoSuggestions" android:text="" />
    
        <TextView android:id="@+id/editText2" android:layout_width="49dp" android:layout_height="wrap_content" android:ems="10" android:inputType="textNoSuggestions" android:text="Expiry:" />
    
        <Button android:id="@+id/btnDate2" android:layout_width="59dp" android:layout_height="wrap_content" android:layout_weight="0.41" android:ems="10" android:onClick="setDate2" android:text="" />
    
        <Button android:id="@+id/button9" android:layout_width="59dp" android:layout_height="wrap_content" android:layout_weight="0.41" android:ems="10" android:inputType="textNoSuggestions" android:onClick="diff" android:text="calc" />
      </LinearLayout>
    

    修改了代码。

    第一次点击, enter image description here

    选择“确定”后。 enter image description here

    现在我应该再次单击“计算”按钮来了解天数差异。 enter image description here

    我需要的是先点击日期,然后在选择日期时选择“确定”。我应该能够在按屏幕上的“确定”时看到天数差异,而不是再次按下按钮。

    要求3:

    当Edittext2数字超过vs spinner2数字时,Edittext2颜色应将其更改为红色并显示一些Doast消息。 enter image description here

    当Edittext2数字接近vs spinner2数字时,Edittext2颜色应将其更改为黄色并显示一些Doast消息。 enter image description here

    1 回复  |  直到 6 年前
        1
  •  1
  •   i_A_mok    6 年前

    1.在mainactivity.java中添加以下方法:

    public void dualFunctions(View view) {
        if(view.getTag() == null) view.setTag("0");
        if(view.getTag().equals("0")){
            view.setTag("1");
            view.setBackgroundColor(Color.GREEN);
            setDate2(view);
        }else{
            view.setTag("0");  // Remove this line for one shoot.
            view.setBackgroundColor(Color.YELLOW);
            diff(view);
        }
    }
    

    2.更改布局中的按钮以使用此方法。

    该方法在日期选择和计算之间切换。如果只允许日期选择一次,则删除带注释的行。

    希望有帮助!

    更新时间:

    public void onDateSet(DatePicker view, int year, int month, int day) {
        Button btnDate2 = (Button) getActivity().findViewById(R.id.btnDate2);
        String stringOfDate = day + "." + (month + 1) + "." + year;
        btnDate2.setText(stringOfDate);
        MainActivity activity = (MainActivity)getActivity();
        activity.diff(btnDate2);
    }
    

    要求3:

    如果不是,则添加以检查条件。

    private final static int VERY_NEAR_DATE = 30;
    
    public void diff(View view) {
        SimpleDateFormat myFormat = new SimpleDateFormat("dd.MM.yyyy");
        CharSequence inputString1 = editTextDate5.getText();
        try {
            LocalDate date1 = LocalDate.parse(inputString1, dateFormatter);
            CharSequence inputString2 = button2.getText();
            try {
                LocalDate date2 = LocalDate.parse(inputString2, dateFormatter);
                long diffDate = ChronoUnit.DAYS.between(date1, date2);
                txtResult.setText(String.valueOf(diffDate));
    
                diffDate = (Long)ok2.getSelectedItem() - diffDate;
                if(diffDate < 0){
                    txtResult.setTextColor(Color.RED);
                    Toast.makeText(this, "Date not allowed!!", Toast.LENGTH_SHORT).show();
                }else if(diffDate < VERY_NEAR_DATE) {
                    txtResult.setTextColor(Color.YELLOW);
                    Toast.makeText(this, "Date too near!", Toast.LENGTH_SHORT).show();
                }
    
            } catch (DateTimeParseException dtpe) {
                Toast.makeText(this, "Date2 is not a valid date: " + inputString2, Toast.LENGTH_SHORT).show();
            }
        } catch (DateTimeParseException dtpe) {
            Toast.makeText(this, "Date1 is not a valid date: " + inputString1, Toast.LENGTH_SHORT).show();
        }
    }
    

    如果在微调器中更改选择后需要更新视图,则在onCreate()中添加以下代码:

        ok2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                diff(null);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        });