简体   繁体   English

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

[英]Different ID how will call on same method in android

This is my XML code 这是我的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>

when you press this Two EditText(issue date and Expiry Date). 当您按此两个EditText(发行日期和到期日期)时。 I want to show datepicker. 我想显示日期选择器。

This above code are properly working for single EditText(issue date).how to implement for both EditText?? 上面的代码对于单个EditText(发布日期)正常工作。如何为两个EditText实施?

This is my Main Activity.Java code 这是我的主要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");

            }
        }
    });
}

This is my DateDialog.java Code 这是我的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);
}    
}

Just make two instances of you edittext and sign listeners to them. 只需为您创建两个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");

            }
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让6个按钮在Android中使用不同的参数调用相同的方法? - How can I let 6 buttons call the same method with different parameters in Android? 如何在Android中通过其他活动调用方法 - How to call a method from a different activity in Android 如何从oncreate方法android内部调用同一类中的方法? - How to call a method in same class from inside oncreate method android? 如何从同一个Object的不同变量中调用一些方法? - How to call some method from different variables that from same Object? 如何从同一个java项目中的不同类调用另一个方法 - How to call another method from a different class in the same java project 如何让 Mockito 调用不同的方法但返回相同的对象实例 - How to make Mockito call different method but return same object instance 如何使用三个不同的线程调用同一方法 - How can I call a same method with three different thread 如何在Java中为两个不同的对象调用相同的方法 - How to call the same method for two different objects in Java 如何以多线程方式调用不同类的相同方法 - How to call same method of a different class in multithreaded way 如何从java中的两个不同类调用相同的方法 - How to call same method from two different classes in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM