gpt4 book ai didi

android - 单键多 Action 顺序

转载 作者:行者123 更新时间:2023-11-29 23:54:06 25 4
gpt4 key购买 nike

我正在使用一个按钮来完成两项任务。

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

原样:

我正在为两个按钮使用“onclick”方法。

  1. btnDate2用于选择日期和onclick是(setDate2)
  2. button9 用于计算两个日期和onclick 之间的差异是(diff)

future :

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

当前代码:主 Activity .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);
}
}

activity_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 数量超过 spinner2 数量时,edittext2 颜色应将其更改为红色和一些 doast 消息 enter image description here

当 edittext2 数字非常接近 spinner2 数字时,edittext2 颜色应将其更改为黄色和一些 doast 消息 enter image description here

最佳答案

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:

添加了 if-else 以检查条件。

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

如果需要在微调器中更改选择后更新 View ,则在 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) {}
});

关于android - 单键多 Action 顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50555459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com