Activity에 시계가 필요해서
찾던중 디지털시계를 사용하려면 DigitalClock이라는 위젯이 있어서 사용하던중
시간변화에 따라 어떤 특정 작업을 하고싶어서
찾아보니 addTextChangedListener를 사용하면 된다고 한다
/////////////////////////////////////
public class TestActivity extends BaseActivity {
private TextView stop;
private DigitalClock currentTime;
@Override
protected void createActivity() {
View view = this.setContainerView(R.layout.activity_bus);
upStop = (TextView) view.findViewById(R.id.up_stop);
currentTime = (DigitalClock) view.findViewById(R.id.current_time);
currentTime.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
upStop.setText(s);
}
});}
}
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:gravity="center"
>
<TextView android:id="@+id/upStop"android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="40sp"
android:text="test"
android:textStyle="bold"
/>
<DigitalClock
android:id="@+id/current_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-20dp"
android:textSize="90sp"
/>
</LinearLayout>
이렇게 하면 afterTetChanged에 있는 upStop.setText(s); 가
시간이 변할 때 마다 작동하게 됩니다.
/////////////////////////////////////////////////////////
추가
@Override
public void afterTextChanged(Editable s) {
upStop.setText(s);
}
이부분에서 Editable s는 현재 시스템 시간을 가져오는건데
폰에서 설정을 어떻게 하느냐에 따라 시간 포맷이 다릅니다
예를들어 시스템시간을 '24시간 형식 사용' 설정으로 해놓으면
16:00시 18:12 이런식으로 나오지만
안해놓으면 오후 4:00 오후 6:12 이런식으로 나오니
주의해야 합니다
'공부 > Android' 카테고리의 다른 글
(android) button array onclick [많은 버튼을 배열로 만들기] (0) | 2016.06.06 |
---|---|
(android) 다음 지도 API 좌표찾기 (1) | 2016.05.29 |
(android) setimageresource null [src 이미지 비우기] (0) | 2016.05.11 |
HoloColorPicker 값 가져오기 HEX값 (0) | 2016.05.11 |
(android volley) 공통화 하기 (0) | 2016.05.08 |