从Timepicker的编辑文本获取时间 [英] Get time from Timepicker's edit Text

查看:397
本文介绍了从Timepicker的编辑文本获取时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过的旧帖子和所有的答案建议clearFocus。

I've seen older posts and all the answers suggest to clearFocus.

我不能这样做,因为我的TimePicker不是一个对话框里面,所以我不知道什么时候我应该叫clearFocus()函数,可能会崩溃我的应用程序,如果我想尝试之后修改TimePicker。

I can't do that, because my TimePicker isn't inside a dialog, so I don't know when I should call clearFocus() function and probably it will crash my app if I would try to modify TimePicker after.

我敢肯定,这个问题有一个解决方案,因为它配备了Android的报警程序做这个功能没有一个对话框。

I'm sure this problem has a solution because the Alarm app which comes with Android do this feature without a dialog.

任何答案是值得欢迎的,
问候!

Any answer is welcome, Regards!

推荐答案

您应该使用 TimePicker.OnTimeChangedListener 来处理 onTimeChanged 行动,如果用户已经改变了数小时或数分钟值通过点击加或减,或编辑工作时使用manualy键盘。

You should use TimePicker.OnTimeChangedListener to handle onTimeChanged action if the user has changed hours or minutes values by tapping on plus or minus, or by editting the time manualy using keyboard.

这是我的code

活动:

public class MainActivity extends Activity 
    implements TimePicker.OnTimeChangedListener {

    private TextView resultTime;

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

        resultTime = (TextView) findViewById(R.id.activity_main_textview_resulttime);

        TimePicker timePicker = (TimePicker) findViewById(R.id.activity_main_timepicker);
        {    
            timePicker.setIs24HourView(true);
            timePicker.setOnTimeChangedListener(this);
        }
    }

    @Override
    public void onTimeChanged(TimePicker timePickerView, int hours, int minutes) {
        final String stringNewTime = hours + " : " + minutes;
        resultTime.setText(stringNewTime);
    }   
}

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical" >

    <TimePicker
        android:id="@+id/activity_main_timepicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="14dip"
        android:layout_marginRight="16dip"
        android:focusable="true"
        android:focusableInTouchMode="true" />

    <TextView
        android:id="@+id/activity_main_textview_resulttime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

希望,它会帮助你!

Hope, it will help you!

这篇关于从Timepicker的编辑文本获取时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆