不同的ID如何在android中调用相同的方法

时间:2017-03-15 08:05:43

标签: java android xml

这是我的XML代码

   <ScrollView
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.example.dk.azenna.Document">

   <LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="50dp"
        android:focusableInTouchMode="true">

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="500px"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_margin="16dp"/>

        <EditText
            android:id="@+id/issue"
            android:layout_width="500px"
            android:layout_height="wrap_content"
            android:layout_below="@+id/spinner"
            android:layout_centerHorizontal="true"
            android:layout_margin="16dp"
            android:hint="Issue Date"
            android:onClick="buttonOnClick"/>
        <EditText
            android:id="@+id/expiry"
            android:layout_width="500px"
            android:layout_height="wrap_content"
            android:layout_below="@id/issue"
            android:layout_centerHorizontal="true"
            android:layout_margin="16dp"
            android:hint="Expiry Date"
            android:onClick="buttonOnClick"/>          

     </RelativeLayout>
     </LinearLayout>
     </ScrollView>

按下此两个EditText(发布日期和到期日期)。我想显示datepicker。

以上代码适用于单个EditText(发布日期)。如何实现EditText?

这是我的主Activity.Java代码

  public void onStart(){
    super.onStart();
    EditText txtDate=(EditText)findViewById(R.id.issue );
    txtDate.setOnFocusChangeListener(new View.OnFocusChangeListener(){
        public void onFocusChange(View view, boolean hasfocus){
            if(hasfocus){
                DateDialog dialog=new DateDialog(view);
                FragmentTransaction ft =getFragmentManager().beginTransaction();
                dialog.show(ft, "DatePicker");

            }
        }
    });
}

这是我的DateDialog.java代码

 @SuppressLint("ValidFragment")
 public class DateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener {
EditText txtdate;
public DateDialog(View view){
    txtdate=(EditText)view;
}
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) {
    String date=day+"-"+(month+1)+"-"+year;
    txtdate.setText(date);
}    
}

1 个答案:

答案 0 :(得分:0)

只需创建两个edittext实例并为其签名。

public void onStart(){
    super.onStart();
//first edittext
    EditText txtDate =(EditText)findViewById(R.id.issue);
    txtDate.setOnFocusChangeListener(new View.OnFocusChangeListener(){
        public void onFocusChange(View view, boolean hasfocus){
            if(hasfocus){
                DateDialog dialog=new DateDialog(view);
                FragmentTransaction ft =getFragmentManager().beginTransaction();
                dialog.show(ft, "DatePicker");

            }
        }
    });
//second edittext
    EditText txtDate2 =(EditText)findViewById(R.id.expiry);
    txtDate.setOnFocusChangeListener(new View.OnFocusChangeListener(){
        public void onFocusChange(View view, boolean hasfocus){
            if(hasfocus){
                DateDialog dialog=new DateDialog(view);
                FragmentTransaction ft =getFragmentManager().beginTransaction();
                dialog.show(ft, "DatePicker");

            }
        }
    });
}