我正在构建需要这种复杂机制的应用程序。请帮帮我。当我单击图像按钮时,它应该显示一个弹出窗口。在这个弹出窗口中,我保留了两个 editText。我已将两个 editTexts 都设置为 OnclickListener。现在,当我单击 edittext 时,它应该显示 DatePicker,最后当我在 datepicker 上设置日期时,它应该在那些 editTexts 上设置。
我不知道该怎么做。阅读一些教程,我使我的 java 代码看起来像:
package com.addictioncounterapp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
public class AddictionDetails extends Activity
{
TextView tv_addiction_name, tv_today, tv_yesterday, tv_this_week, tv_this_month, tv_total;
ImageView iv_back, iv_filter;
int year, month, day;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addiction_details);
tv_addiction_name = (TextView) findViewById(R.id.textViewAddictionDetails);
tv_total = (TextView) findViewById(R.id.textview_total);
tv_today = (TextView) findViewById(R.id.textview_today);
tv_yesterday = (TextView) findViewById(R.id.textview_yesterday);
tv_this_week = (TextView) findViewById(R.id.textview_this_week);
tv_this_month = (TextView) findViewById(R.id.textview_this_month);
iv_back = (ImageView) findViewById(R.id.imageViewAddictionDetailsBack);
iv_back.setClickable(true);
iv_back.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(AddictionDetails.this, StartActivity.class);
startActivity(intent);
}
}
);
iv_filter = (ImageView) findViewById(R.id.imageViewAddictionDetailsFilter);
iv_filter.setClickable(true);
iv_filter.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.activity_filter_addiction_form, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final EditText etStartDate = (EditText)popupView.findViewById(R.id.editTextFilterStartDate);
etStartDate.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
final int DATE_DIALOG_ID = 1;
showDialog(DATE_DIALOG_ID);
protected Dialog onCreateDialog(int id)
{
switch(id)
{
case DATE_DIALOG_ID:
return new DatePickerDialog(this, dateListener, year, month, day);
}
return null;
}
}
}
);
final Calendar cal = Calendar.getInstance();
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH);
day = cal.get(Calendar.DAY_OF_MONTH);
etStartDate.setText(new StringBuilder().append(day).append('/').append(month + 1).append('/').append(year));
DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int yr, int monthOfYear, int dayOfMonth)
{
year = yr;
month = monthOfYear;
day = dayOfMonth;
etStartDate.setText(new StringBuilder().append(day).append('/').append(month + 1).append('/').append(year));
}
};
EditText etEndDate = (EditText)popupView.findViewById(R.id.editTextFilterEndDate);
etEndDate.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
}
}
);
ImageView iv_ok = (ImageView) popupView.findViewById(R.id.imageViewOk);
iv_ok.setClickable(true);
iv_ok.setOnClickListener(new ImageView.OnClickListener()
{
@Override
public void onClick(View v)
{
}
}
);
ImageView iv_cancel = (ImageView) popupView.findViewById(R.id.imageViewCancel);
iv_cancel.setClickable(true);
iv_cancel.setOnClickListener(new ImageView.OnClickListener()
{
@Override
public void onClick(View v)
{
popupWindow.dismiss();
}
}
);
}
}
);
Intent intent = getIntent();
String cat_name = intent.getStringExtra("cat_name");
tv_addiction_name.setText(cat_name);
SQLiteDatabase db = openOrCreateDatabase("AddictionCounter.db", SQLiteDatabase.OPEN_READWRITE, null);
Cursor cursor = db.query("category", new String[]{"cat_id"}, "cat_name=?", new String[]{cat_name}, null, null, null);
//----------------------fetching id-------------------
int cat_id = 0;
if(cursor.getCount() > 0)
while(cursor.moveToNext())
cat_id = cursor.getInt(0);
cursor.close();
//----------------------fetching total-------------------
int total;
cursor = db.query("counter", new String[]{"cat_id"}, "cat_id=?", new String[]{cat_id+""}, null, null, null);
total = cursor.getCount();
tv_total.setText(total+"");
//----------------------fetching today's count---------------
cursor = db.query("counter", new String[]{"counter_entry_date"}, "cat_id=?", new String[]{cat_id+""}, null, null, null);
Calendar cal1 = Calendar.getInstance();
SimpleDateFormat dateFormat1 = new SimpleDateFormat("dd/MM/yyyy");
String todays_date = dateFormat1.format(cal1.getTime());
String cursor_date;
int counter = 0;
if(cursor.getCount() > 0)
{
while(cursor.moveToNext())
{
cursor_date = cursor.getString(0)+"";
if(cursor_date.equals(todays_date))
counter++;
}
}
tv_today.setText(counter+"");
//----------------------fetching yesterdays count------------------
Calendar cal2 = Calendar.getInstance();
SimpleDateFormat dateFormat2 = new SimpleDateFormat("dd/MM/yyyy");
cal2.add(Calendar.DATE, -1);
String yesterdays_date = dateFormat2.format(cal2.getTime());
counter = 0;
cursor.moveToFirst();
if(cursor.getCount() > 0)
{
while(cursor.moveToNext())
{
cursor_date = cursor.getString(0)+"";
if(cursor_date.equals(yesterdays_date))
counter++;
}
}
tv_yesterday.setText(counter+"");
//-------------------------fetching current week count--------------------
ArrayList<String> current_week_dates = new ArrayList<String>();
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
for (int i = 0; i < 7; i++)
{
current_week_dates.add(df.format(c.getTime()));
c.add(Calendar.DATE, 1);
}
counter=0;
cursor.moveToFirst();
while(cursor.moveToNext())
{
if(current_week_dates.contains(cursor.getString(0)))
{
counter++;
}
}
tv_this_week.setText(counter+"");
//----------------------fetching current month count--------------------
cursor.moveToFirst();
counter=0;
Date date = new Date();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("MM/yyyy");
String current_month= sdf.format(date);
// Toast.makeText(getBaseContext(), current_month, Toast.LENGTH_SHORT).show();
while(cursor.moveToNext())
{
if((cursor.getString(0)).matches("(.*)"+current_month))
{
counter++;
}
}
// Toast.makeText(getBaseContext(), counter+"", Toast.LENGTH_SHORT).show();
tv_this_month.setText(counter+"");
cursor.close();
}
}
这是我的主要 xml 文件,其中包含 imageview,从中显示弹出窗口:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/black_red_texture" >
<ImageView
android:id="@+id/imageViewAddictionDetailsBack"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/arrow_left"
android:clickable="true" />
<TextView
android:id="@+id/textViewAddictionDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_centerHorizontal="true"
android:text="Addiction Details"
android:textSize="20sp" />
<TableLayout
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="false"
android:layout_below="@id/textViewAddictionDetails"
android:layout_centerHorizontal="true"
android:background="#20ffffff"
android:shrinkColumns="1" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:textColor="#ffffff"
android:text="Today:" />
<TextView
android:textColor="#ffffff"
android:id="@+id/textview_today"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:text="today" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp" >
<TextView
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="Yesterday:" />
<TextView
android:textColor="#ffffff"
android:id="@+id/textview_yesterday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:text="yesterday" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp" >
<TextView
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="This Week:" />
<TextView
android:textColor="#ffffff"
android:id="@+id/textview_this_week"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:text="this week" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp" >
<TextView
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="This Month:" />
<TextView
android:textColor="#ffffff"
android:id="@+id/textview_this_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:text="this month" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
<TextView
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="Total:" />
<TextView
android:textColor="#ffffff"
android:id="@+id/textview_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:text="total" />
</TableRow>
</TableLayout>
<ImageView
android:id="@+id/imageViewAddictionDetailsFilter"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/filter" />
</RelativeLayout>
这是我的弹出布局 xml 代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#20000000"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="Start Date : "
android:textColor="#ffffff" />
<EditText
android:id="@+id/editTextFilterStartDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:hint="Select start date"
android:clickable="true"
android:focusableInTouchMode="false" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="End Date : "
android:textColor="#ffffff" />
<EditText
android:id="@+id/editTextFilterEndDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:hint="select end date"
android:clickable="true"
android:focusableInTouchMode="false" />
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageViewCancel"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_weight="1"
android:src="@drawable/buttons_cancel"
android:clickable="true" />
<ImageView
android:id="@+id/imageViewOk"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_weight="1"
android:src="@drawable/buttons_ok"
android:clickable="true" />
</LinearLayout>
</LinearLayout>
这是我的日期选择器布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<DatePicker
android:id="@+id/DatePicker01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></DatePicker>
</LinearLayout>
我没有运行代码,因为它在 java 代码中的某些行或其他行上显示错误,而我自己被困住了,使这变得更加复杂。