如何单击TextView项时弹出DatePicker

时间:2014-11-05 04:23:37

标签: android eclipse datepicker

我是Eclipse新手并运行Juno。当我试图在文本项目上实现日期选择器时,我发现了移动应用程序。我已经从stackoverflow中读到类似的问题并应用了答案,但它似乎对我不起作用。我不断做出改变,我完全迷失了。我尝试添加日历小部件,但它比屏幕更大,所以我只想显示一个带日历的视图供选择。

我需要我的用户能够选择他们想要旅行的日期,并且当点击搜索按钮时,表单中的条目用于显示他们可以预订的可用巴士列表。请在下面找到我的代码,没有帮助太少。

SEARCHBUS_ACTIVITY.JAVA

package com.joel.mybusapp;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.FragmentActivity;

public class SearchBusActivity extends ActionBarActivity {

private Spinner fromSpinner, toSpinner;
private Button searchBusButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_bus);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);      
    addListenerOnFromSpinnerItemSelection();
    addListenerOnToSpinnerItemSelection();
}

public void addListenerOnFromSpinnerItemSelection() {
    fromSpinner = (Spinner) findViewById(R.id.fromSpinner);
    fromSpinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
  }
public void addListenerOnToSpinnerItemSelection() {
    toSpinner = (Spinner) findViewById(R.id.toSpinner);
    toSpinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
  }

Calendar myCalendar = Calendar.getInstance();

DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        // TODO Auto-generated method stub
        myCalendar.set(Calendar.YEAR, year);
        myCalendar.set(Calendar.MONTH, monthOfYear);
        myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateLabel();
    }

};
TextView textViewDatePicker = (TextView) findViewById(R.id.textDateView);

textViewDatePicker.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        new DatePickerDialog(SearchBusActivity.this, date, myCalendar
                .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                myCalendar.get(Calendar.DAY_OF_MONTH)).show();
    }
});
private void updateLabel() {

    String myFormat = "MM/dd/yy"; //In which you need put here
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

    textViewDatePicker.setText(sdf.format(myCalendar.getTime()));
    }
public void addListenerOnButton() {

    fromSpinner = (Spinner) findViewById(R.id.fromSpinner);
    toSpinner = (Spinner) findViewById(R.id.toSpinner);
    searchBusButton = (Button) findViewById(R.id.searchBusButton);

    searchBusButton.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {

        Toast.makeText(SearchBusActivity.this,
        "OnClickListener : " + 
                "\nOrigin : "+ String.valueOf(fromSpinner.getSelectedItem()) + 
                "\nDestination : "+ String.valueOf(toSpinner.getSelectedItem()),
            Toast.LENGTH_SHORT).show();
      }

    });
  }       

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.search_bus, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

ACTIVITY_SEARCH_BUS.XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.joel.mybusapp.SearchBusActivity" >

<TextView
    android:id="@+id/depatureTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="24dp"
    android:text="@string/departure"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#E81919"
    android:textStyle="bold" />

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/depatureTextView"
    android:layout_marginStart="20dp"
    android:layout_marginTop="5dp" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/fromTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#DCD9D9"
            android:text="@string/from"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#E81919"
            android:textStyle="bold" />

        <Spinner
            android:id="@+id/fromSpinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:entries="@array/from_arrays"
            android:prompt="@string/origin_prompt" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/toTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#DCD9D9"
            android:text="@string/to"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#E81919"
            android:textStyle="bold" />

        <Spinner
            android:id="@+id/toSpinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:entries="@array/to_arrays"
            android:prompt="@string/destination_prompt" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/dateTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#DCD9D9"
            android:text="@string/date"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#E81919"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textDateView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            style="@android:style/Widget.DeviceDefault.Light.Spinner"/>

    </TableRow>

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/passengersTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#DCD9D9"
            android:text="@string/passengers"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#E81919"
            android:textStyle="bold" />

        <Spinner
            android:id="@+id/passengerSpinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:entries="@array/passenger_arrays"
            android:prompt="@string/passenger_prompt" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableRow>
</TableLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"        
    android:layout_below="@+id/tableLayout1"
    android:layout_centerInParent="true"
    android:layout_marginStart="14dp"       
    android:orientation="vertical" >

    <Button
        android:id="@+id/searchBusButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"            
        android:text="@string/searchbusbtn" />

</LinearLayout>

</RelativeLayout>

SCREENSHOT

searchBus

1 个答案:

答案 0 :(得分:0)

第1步

为你的textview

创建onClickListener

第2步

onClickListener中的

使用我用于datepickerdialog

的代码

inOnClick方法

setDate();

setDate方法:

public void setDate() {
    new DatePickerDialog(add_task_frag.this, d,
            calender.get(Calendar.YEAR), calender.get(Calendar.MONTH),
            calender.get(Calendar.DAY_OF_MONTH)).show();
}

DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker arg0, int year, int month, int day) {

        calender.set(Calendar.YEAR, year);
        calender.set(Calendar.MONTH, month);
        calender.set(Calendar.DAY_OF_MONTH, day);
        updatedate();

    }
};

updatedate()方法:

public void updatedate() {
    textview.setText(format.format(calender.getTime()));
}