0

我有一个带有一点“数量”editext 的 DateTimePicker,我保存了 Sharedpref 但由于某种原因,如果我“触摸”EditText 并手动输入数量,文本是黑色的并且不会传输到 TextView,如果使用imagebuttonsincreasedecreaseeditText 将按预期工作。如果我以这种方式输入日期和时间,虽然它确实有效,但我错过了什么?

ImageButton increase = (ImageButton)mDateTimeDialogView.findViewById(R.id.increase);
increase.setOnClickListener(new OnClickListener(){
public void onClick(View v){


amountt++;
amountText = Integer.toString(amountt);
amountedit.setHint(amountText);
amnt.setText(amountText);



editor.putString("numbers", amountText);
editor.commit();

    }
});
ImageButton decrease = (ImageButton)mDateTimeDialogView.findViewById(R.id.decrease);
decrease.setOnClickListener(new OnClickListener(){

    public void onClick(View v){




      amountt--;
      amountText = Integer.toString(amountt);
      amountedit.setHint(amountText);
      amnt.setText(amountText);

      editor.putString("numbers", amountText);
      editor.commit();

    }
});

相关的 EditText 信息

amountedit=(EditText)mDateTimeDialogView.findViewById(R.id.amountedit);

它显示的 TextView

amnt = (TextView)findViewById(R.id.amountText);

EditText 的 XML

<EditText
    android:id="@+id/amountedit"
    android:layout_width="50dp"
    android:key = "amntkey"
    android:layout_height="wrap_content"
    android:layout_above="@+id/linearLayout1"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="62dp"
    android:ems="10"
    android:inputType="number"
    android:persistent="true" />

它发生的完整java活动:

import java.util.Calendar;
import java.util.HashMap;

import org.iimed.www.DateTimePicker.DateWatcher;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.inputmethodservice.Keyboard.Key;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class CaAdd extends Activity implements DateWatcher,OnClickListener{
Button button_click;
int amountt = 0;

String amountText;
String number = "numbers";
TextView amnt;
TextView dateResult, date2;
int hrs,min;
SharedPreferences pref;
String time = "time";
String get;
String sTime;
String amount ;
EditText amountedit;
String result_string;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datetimeactivity);
dateResult = (TextView)findViewById(R.id.textview1);

date2 = (TextView)findViewById(R.id.textView2);
 button_click = (Button)findViewById(R.id.setbtn);
 pref = getSharedPreferences("prefs", Context.MODE_PRIVATE);
button_click.setOnClickListener(this);
 amnt = (TextView)findViewById(R.id.amountText);
 pref = getSharedPreferences("prefs", Context.MODE_PRIVATE);
 amountText = Integer.toString(amountt);
 amount = amnt.toString();
 get = pref.getString("numbers",amountText);
sTime = pref.getString("time", result_string);
 amnt.setText(get);
 date2.setText(sTime) ; 
}



@Override
public void onClick(View v) {
    switch (v.getId()){


    case R.id.setbtn:




final Dialog mDateTimeDialog = new Dialog(this);
// Inflate the root layout
final RelativeLayout mDateTimeDialogView = (RelativeLayout) getLayoutInflater().inflate(R.layout.date_time_dialog, null);
// Grab widget instance
final DateTimePicker mDateTimePicker = (DateTimePicker) mDateTimeDialogView.findViewById(R.id.DateTimePicker);
mDateTimePicker.setDateChangedListener(this);
mDateTimePicker.initData();
amountedit=(EditText)mDateTimeDialogView.findViewById(R.id.amountedit);
ImageButton increase = (ImageButton)mDateTimeDialogView.findViewById(R.id.increase);
increase.setOnClickListener(new OnClickListener(){
    public void onClick(View v){


        Editor editor = pref.edit();

amountt++;
amountText = Integer.toString(amountt);
amountedit.setHint(amountText);
amnt.setText(amountText);

String amount =amnt.toString();

editor.putString("numbers", amountText);
editor.commit();

    }
});
ImageButton decrease = (ImageButton)mDateTimeDialogView.findViewById(R.id.decrease);
decrease.setOnClickListener(new OnClickListener(){

    public void onClick(View v){



        Editor editor = pref.edit();
      amountt--;
      amountText = Integer.toString(amountt);
      amountedit.setHint(amountText);
      amnt.setText(amountText);
String edi = amountedit.toString();
      editor.putString("numbers", amountText);
      editor.commit();

    }
});

Button setDateBtn = (Button)mDateTimeDialogView.findViewById(R.id.SetDateTime);

setDateBtn.setOnClickListener(new OnClickListener() {
     public void onClick(View v) {
            Intent intent=new Intent(CaAdd.this,AlarmReceiver.class);
            PendingIntent pi=PendingIntent.getActivity(CaAdd.this, 2,    intent,PendingIntent.FLAG_CANCEL_CURRENT);
           AlarmManager alm=(AlarmManager) getSystemService(Context.ALARM_SERVICE);


            Editor editor = pref.edit();

            hrs=mDateTimePicker.getHour();
            min=mDateTimePicker.getMinute();
            Calendar calendar=Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY,hrs);
            calendar.set(Calendar.MINUTE, min);
            calendar.set(Calendar.SECOND, 0);
            alm.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), pi);

         mDateTimePicker.clearFocus();
           result_string = 
         String.valueOf(mDateTimePicker.getHour()) + ":" + String.valueOf(mDateTimePicker.getMinute());


          if(mDateTimePicker.getHour() > 12) result_string = result_string + "PM";
         else result_string = result_string + "AM";
         date2.setText(result_string);
         editor.putString("time",result_string);
          editor.commit();

         mDateTimeDialog.dismiss();
         }
         });




Button cancelBtn = (Button)mDateTimeDialogView.findViewById(R.id.CancelDialog);

cancelBtn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
mDateTimePicker.reset();
mDateTimeDialog.cancel();
}
});



Button resetBtn = (Button)mDateTimeDialogView.findViewById(R.id.ResetDateTime);
resetBtn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
mDateTimePicker.reset();
}
});

mDateTimeDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

mDateTimeDialog.setContentView(mDateTimeDialogView);

mDateTimeDialog.show();}}




public void onDateChanged(Calendar c) {
Log.e("","" + c.get(Calendar.MONTH) + " " + c.get(Calendar.DAY_OF_MONTH)+ " " + c.get(Calendar.YEAR));
String result_string =String.valueOf(c.get(Calendar.MONTH)+1)  + "/" + String.valueOf(c.get(Calendar.DAY_OF_MONTH)) + "/" + String.valueOf(c.get(Calendar.YEAR));

dateResult.setText(null);







}}
4

1 回答 1

0

您的代码不清楚,您应该发布有关您正在谈论的 EditText 的代码。

顺便说一句,要得到你想要的,你可以尝试做 Sharedpref 的编辑和提交

当你的 EditText 的文本发生变化时,虽然它的设计很糟糕......

您需要一个文本更改侦听器

EditText.addTextChangedListener(new TextWatcher()
{
  @Override
  public void onTextChanged(CharSequence s, int start, int before, int count)
  {}

  @Override
  public void beforeTextChanged(CharSequence s, int start, int count, int after)
  {}

  @Override
  public void afterTextChanged(Editable s)
  {}
});
于 2013-11-14T03:46:52.717 回答