TextWatcher不工作,有什么不对

时间:2013-03-25 13:36:51

标签: android listener textwatcher

在onCreate方法中TextWatcher的实现不起作用,我尝试使用虚拟键盘和硬件键盘进行文本输入,也尝试了模拟器和真实设备上的代码。似乎没什么用。

我甚至在布局中重命名了EditTexts并清理了项目,重新构建它,没有希望

任何想法?

这是代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.remarks);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    Intent i = getIntent();
    Current_Language = i.getStringExtra("Lang");
    Q1Rate = i.getFloatExtra("Q1Rate", 0f);
    Q2Rate = i.getFloatExtra("Q2Rate", 0f);
    Q3Rate = i.getFloatExtra("Q3Rate", 0f);
    Q4Rate = i.getFloatExtra("Q4Rate", 0f);
    Q5Rate = i.getFloatExtra("Q5Rate", 0f);
    LoadSettings();
    NextBtnState();
    final EditText edt_PhoneNo     = (EditText) findViewById(R.id.edt_Remarks_PhoneNo);
    final EditText edt_Suggestions = (EditText) findViewById(R.id.edt_Remarks_Suggestion);
    edt_Suggestions.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        public void afterTextChanged(Editable s) {
            ShowToast("I Am Here Firas");
            resetTimeout();
        }
    });
    edt_PhoneNo.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        public void afterTextChanged(Editable s) {
            ShowToast("I Am Here Firas");
            resetTimeout();
        }
    });
}

这是我的ShowToast

public void ShowToast (String Msg) {
    Context context = getApplicationContext();
    CharSequence text = Msg;
    int duration = Toast.LENGTH_LONG;
    Toast toast = Toast.makeText(context, text, duration);
    toast.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 0);
    toast.show();
}

这是布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/Azure1"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center_vertical"
android:orientation="vertical" >

<GridLayout
    android:id="@+id/Grid1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:columnCount="4"
    android:rowCount="4" >

    <Space
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/img_ClientLogo"
        android:layout_width="275dp"
        android:layout_height="145dp"
        android:layout_gravity="left"
        android:src="@drawable/general_client_logo" />

    <TextView
        android:id="@+id/tv_CustomerSatisfactionSurvey"
        android:layout_width="948dp"
        android:layout_height="103dp"
        android:layout_column="3"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:layout_row="0"
        android:gravity="center"
        android:text="@string/Customer_Satisfaction_Survey"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/OrangeRed2"
        android:textSize="50sp" />
</GridLayout>

<RelativeLayout
    android:id="@+id/ly_Stars"
    android:layout_width="match_parent"
    android:layout_height="379dp">

    <EditText
        android:id="@+id/edt_Remarks_PhoneNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_Remarks_PhoneNo"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:ems="10"
        android:hint="@string/Mobile_No"
        android:inputType="phone" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/tv_Remarks_Suggestions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edt_Remarks_PhoneNo"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:text="@string/Any_Comment"
        android:textSize="25sp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/edt_Remarks_Suggestion"
        android:layout_width="590dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_Remarks_Suggestions"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:ems="10"
        android:hint="@string/suggestion"
        android:inputType="textCapSentences|textMultiLine"
        android:singleLine="false" />

    <TextView
        android:id="@+id/tv_Remarks_PhoneNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:text="@string/Phone_Account_No"
        android:textSize="25sp"
        android:textStyle="bold" />

</RelativeLayout>

<RelativeLayout
    android:id="@+id/ly_NextPrevios"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom" >

    <Button
        android:id="@+id/btn_Next"
        style="@style/ButtonText"
        android:layout_width="250dp"
        android:layout_height="100dp"
        android:layout_above="@+id/tv_PoweredBy"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="45dp"
        android:background="@drawable/btn_blue"
        android:drawableRight="@drawable/arrow_right"
        android:onClick="Next_Page"
        android:text="@string/btn_Next" />

    <TextView
        android:id="@+id/tv_PoweredBy"
        android:layout_width="772dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="@string/powerd_by_prime"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="16sp" />

    <Button
        android:id="@+id/btn_Previous"
        style="@style/ButtonText"
        android:layout_width="250dp"
        android:layout_height="100dp"
        android:layout_above="@+id/tv_PoweredBy"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="45dp"
        android:background="@drawable/btn_blue"
        android:drawableLeft="@drawable/arrow_left"
        android:onClick="Previous_Page"
        android:text="@string/btn_Previous" />

    <ImageView
        android:id="@+id/img_help"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_alignBottom="@+id/btn_Previous"
        android:layout_centerHorizontal="true"
        android:onClick="ShowHelpDialog"
        android:src="@drawable/help_64" />

    <TextView
        android:id="@+id/tv_Steps"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/progressBar1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:text="@string/Step6of6"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="650dp"
        android:layout_height="20dp"
        android:layout_above="@+id/img_help"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:max="6"
        android:progress="6"
        android:progressDrawable="@drawable/my_progress" />

</RelativeLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:3)

EditText的命名与另一个布局冲突 我在两个布局中重命名了EditTexts,然后清理了项目 一切都有效 谷歌必须对不同布局上的Vies命名冲突做些什么

答案 1 :(得分:0)

尝试将上下文从getApplicationContext()更改为ActivityName.this