0

这里是在 TimePickerFragment、DialogFragment 的帮助下使用 imageview 选择到达和退出时间。My question is how to select the exit time..When am selecting exit time it will show in the arrival time text box only. 如何解决这个问题帮助我。

这里到达和退出时间的代码使用两个文本框和两个图像视图按钮。

import java.util.Calendar;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TimePicker;

@SuppressLint("ValidFragment")
public class MainActivity extends FragmentActivity {
EditText arrtime,exittime;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}
public void selectArrivalTime(View view) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getSupportFragmentManager(), "TimePicker");
}
public void selectExitTime(View view) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getSupportFragmentManager(), "TimePicker");

}
    public void populateSetArrTime(int hourOfDay, int minute) {
        arrtime = (EditText)findViewById(R.id.editText1);

        arrtime.setText(+hourOfDay+":"+minute);

    }
    public void populateSetExitTime(int hourOfDay, int minute) {
        exittime= (EditText)findViewById(R.id.editText2);
        exittime.setText(+hourOfDay+":"+minute);
    }
    public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Calendar calendar = Calendar.getInstance();
             int hour =  calendar.get(Calendar.HOUR_OF_DAY);
                int minute = calendar.get(Calendar.MINUTE);

                return new TimePickerDialog(getActivity(), this, hour, minute,
                        DateFormat.is24HourFormat(getActivity()));
        }

        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            // TODO Auto-generated method stub
            populateSetArrTime(hourOfDay, minute);
        }
        public void onTimeSet1(TimePicker view, int hourOfDay, int minute) {
            // TODO Auto-generated method stub
            populateSetExitTime(hourOfDay, minute);
        }
    }

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

}

这是我的 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=".MainActivity" >
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="22dp"
    android:layout_marginTop="67dp"
    android:text="Arrival time" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="30dp"
    android:text="Exit time" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/textView1"
    android:layout_toRightOf="@+id/textView1"
    android:clickable="false"
    android:ems="10"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:inputType="time" />

<EditText
  android:id="@+id/editText2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignBaseline="@+id/textView2"
  android:layout_alignBottom="@+id/textView2"
  android:layout_alignLeft="@+id/editText1"
  android:clickable="false"
  android:ems="10"
  android:focusable="false"
  android:focusableInTouchMode="false"
  android:inputType="time" >

</EditText>

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/editText2"
    android:layout_marginTop="51dp"
    android:contentDescription="@string/selectarrtime"
    android:onClick="selectArrivalTime"
    android:src="@drawable/ic_launcher" />

<ImageButton
    android:id="@+id/imageButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/editText2"
    android:layout_alignTop="@+id/imageButton1"
    android:layout_marginRight="33dp"
    android:contentDescription="@string/selectexittime"
    android:onClick="selectExitTime"
    android:src="@drawable/ic_launcher" />

</RelativeLayout>

如果选择到达时间,我将在我的到达编辑文本框中显示时间,如果我选择退出时间,它将在同一到达编辑文本框中显示时间,而不是在我的退出编辑文本框中显示如何解决这个问题,请帮助我谢谢提前。

4

1 回答 1

0
@SuppressLint("ValidFragment")
public class MainActivity extends FragmentActivity {
EditText arrtime,exittime;
static type="";    //changed

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageButton b1=(ImageButton)findViewById(R.id.imageButton1); //changed from here
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setTime("arrival");
        }
    });
    ImageButton b2=(ImageButton)findViewById(R.id.imageButton2);
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setTime("exit");
        }
    });  ///to here
}
public void setTime(String type) {   //function changed
    MainActivity.type=type;
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getSupportFragmentManager(), "TimePicker");
}    
public void populateTime(int hourOfDay, int minute) {  //function changed
    if(type.equals("arrival") {
        arrtime = (EditText)findViewById(R.id.editText1);
        arrtime.setText(+hourOfDay+":"+minute);
    }
    else if(type.equals("exit") {
        arrtime = (EditText)findViewById(R.id.editText2);
        exittime.setText(+hourOfDay+":"+minute);
    }
}
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar calendar = Calendar.getInstance();
         int hour =  calendar.get(Calendar.HOUR_OF_DAY);
            int minute = calendar.get(Calendar.MINUTE);

            return new TimePickerDialog(getActivity(), this, hour, minute,
                    DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // TODO Auto-generated method stub
        populateTime(hourOfDay, minute);  //changed
    }        
}

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

还要从 xml 文件中的 ImageButtons 中删除行 android:onClick 行

于 2014-04-28T15:21:36.837 回答