如何从编辑文本中获取值作为时间值,以将其保存在共享的首选项中,以将配置文件在该特定时间更改为静音,以及如何在列表视图中显示该时间。`Button AddTime =(Button)findViewById(R.id。添加时间);

    sharedpreferences = getSharedPreferences(MyPREFERENCES,
            Context.MODE_PRIVATE);
    if (sharedpreferences.contains(Stime))
    {
        stime.setText(sharedpreferences.getString(Stime, ""));
    }
    if (sharedpreferences.contains(Etime)) {
        etime.setText(sharedpreferences.getString(Etime, ""));
    }
    stime = (EditText) findViewById(R.id.stime);

    stime.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // Process to get Current Time
            Calendar mcurrentTime = Calendar.getInstance();
            int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
            int minute = mcurrentTime.get(Calendar.MINUTE);
            TimePickerDialog mTimePicker;
            mTimePicker = new TimePickerDialog(TimeBased.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                    stime.setText(selectedHour + ":" + selectedMinute);
                    stime   = (EditText)findViewById(R.id.stime);
                }
            }, hour, minute, true);//Yes 24 hour time
            mTimePicker.setTitle("Select Time");
            mTimePicker.show();

        }
    });

    etime = (EditText) findViewById(R.id.etime);

    etime.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // Process to get Current Time
            Calendar mcurrentTime = Calendar.getInstance();
            int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
            int minute = mcurrentTime.get(Calendar.MINUTE);
            TimePickerDialog mTimePicker;
            mTimePicker = new TimePickerDialog(TimeBased.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                    etime.setText(selectedHour + ":" + selectedMinute);
                    etime   = (EditText)findViewById(R.id.etime);
                }
            }, hour, minute, true);//Yes 24 hour time
            mTimePicker.setTitle("Select Time");
            mTimePicker.show();

        }
    });


    ListView lv = (ListView) findViewById(R.id.addtlist);


    // ArrayList for data row
    alist = new ArrayList<>();
    // SimpleAdapter mapping static data to views in xml file
    adapter = new SimpleAdapter(this, alist, R.layout.row, new String[]{"lst", "let"}, new int[]{R.id.lst, R.id.let});
    lv.setAdapter(adapter);


    AddTime.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // HashMap for data values of each row
            map = new HashMap<>();
            map.put("lst", stime.getText().toString());
            map.put("let", etime.getText().toString());
            alist.add(map);
            // Notify the ListView of data changed
            adapter.notifyDataSetChanged();


        }
    });

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            AlertDialog.Builder adb=new AlertDialog.Builder(TimeBased.this);
            adb.setTitle("Delete?");
            adb.setMessage("Are you sure you want to delete " + position);
            final int positionToRemove = position;
            adb.setNegativeButton("Cancel", null);
            adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    alist.remove(positionToRemove);
                    adapter.notifyDataSetChanged();
                }});
            adb.show();
        }
    });

    Button okButton = (Button) findViewById(R.id.Save);
    okButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            savePreferences();
            TimeBased.this.setResult(RESULT_OK);
            finish();
        }
    });
    Button cancelButton = (Button) findViewById(R.id.Cancel);
    cancelButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            TimeBased.this.setResult(RESULT_CANCELED);
            finish();
        }
    });`

===============>>#1 票数:1 已采纳

哇,这确实是一个无可厚非的问题!

无论如何,如果我是你:

  • 不要使用EditText来让用户输入日期值,永远不会有两个人使用相同的模式
  • 如果需要,使用DialogTimePicker并在TextView中显示结果,单击该TextView会触发弹出窗口
  • 现在,您确实拥有一个有效的Date,使用getTime()将其转换为long值并将其放入您的首选项中

例如,这是我正在使用的

public void openDateDialog() {
    DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
        @Override public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
            if (datePicker.isShown()) {
                //here create your date object with year, month and day and save getTime() to prefs
            }
        }
    };

    DatePickerDialog dialog = new DatePickerDialog(getActivity(), listener, 2000, 5, 5);
    dialog.setTitle("your title here");
    dialog.show();
}

  ask by sarfraz translate from so

未解决问题?本站智能推荐: