我正在尝试在单击Tab Activity.My Tab活动中的edittext时实现弹出日历,如下所示。我需要在返回V之前发布代码。这里是标签活动
package com.artificers.subin.inspection;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by Subin on 13-10-2015.
*/
public class Tab2Fragment extends Fragment {
EditText fromdate;
EditText todate;
// List view
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.tab2_view, container, false);
EditText fromdate=(EditText)V.findViewById(R.id.fromdate);
EditText todate=(EditText)V.findViewById(R.id.todate);
fromdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
// Listview Data
String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
"iPhone 4S", "Samsung Galaxy Note 800",
"Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};
lv = (ListView) V.findViewById(R.id.list_view);
// Adding items to listview
adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, R.id.product_name, products);
lv.setAdapter(adapter);
return V;
}
}
我正在尝试在单击Tab Activity.My Tab活动中的edittext时实现弹出日历,如下所示。我需要在返回V之前发布代码。这里是标签活动
答案 0 :(得分:2)
使用它:
final DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
if (isOkayClicked) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
}
isOkayClicked = false;
}
};
final DatePickerDialog datePickerDialog = new DatePickerDialog(
HomePageActivity.this, R.style.DialogTheme, datePickerListener,
year, month, day); //use your activity name here instead of HomePageActivity
datePickerDialog.getDatePicker().setMaxDate(new Date().getTime()); // setting the max date
datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
"Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
if (which == DialogInterface.BUTTON_NEGATIVE) {
dialog.cancel();
isOkayClicked = false;
}
}
});
datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE,
"Change", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
isOkayClicked = true;
DatePicker datePicker = datePickerDialog
.getDatePicker();
datePickerListener.onDateSet(datePicker,
datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth());
}
}
});
datePickerDialog.setCancelable(false);
datePickerDialog.show();
如果需要,请删除最终关键字..
将其添加到values / styles.xml
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#e9671c</item> // you can change the color here
</style>
答案 1 :(得分:0)
希望这会对你有所帮助
将此信息放入点击事件
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(),&#34; datePicker&#34;);
还在项目中添加以下片段
class DatePickerFragment扩展了DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
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视图,int year,int month,int day){
}
}