EditText in Android doesn't show text when typ

2020-02-06 08:04发布

问题:

I have an activity consisting of 3 EditText fields (amongst other things like TextViews and a few buttons). I also have an AutoCompleteTextView with a list of String's using an ArrayAdapter.

Whenever I test the app in an emulator, I can type when the keyboard is up but it doesn't show the text (it still gives predictions) and the text only appears in the EditText box once the keyboard is closed down. This happens when I test it on my phone, too. However, it works and shows up as you type on the emulator if the on-screen keyboard isn't up and you're just typing normally.

I have no idea why!

Here is my Activity XML (where the EditText's are the top 3 blocks)

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/l"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_x="3dp"
    android:layout_y="5dp"
    android:background="@drawable/gymbg" >


   <AutoCompleteTextView android:id="@+id/inputExercise"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:completionThreshold="1"
        android:inputType="text"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/timeSet"
        android:layout_margin="10dp"
        android:layout_marginTop="50dp"
        android:width="200dp" />  

    <EditText
        android:id="@+id/inputWeight"
        android:inputType="number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/inputExercise"
        android:layout_margin="10dp"
        android:layout_marginTop="50dp"
        android:width="200dp" >
    </EditText>

    <EditText
        android:id="@+id/inputReps"
        android:inputType="number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/inputWeight"
        android:layout_margin="10dp"
        android:layout_marginTop="50dp"
        android:width="200dp" >
    </EditText>

    <TextView
        android:id="@+id/timeMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/timeMain"
        android:textSize="32sp" 
        android:textColor="#0F293B"/>

    <TextView
        android:id="@+id/timeSet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/timeMain"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="@string/timeSet" 
        android:layout_marginBottom="50dp"
        android:textColor="#0F293B"/>

    <TextView
        android:id="@+id/labExercise"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/addbutton"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/timeSet"
        android:layout_centerVertical="true"
        android:layout_margin="10dp"
        android:layout_marginLeft="15dp"
        android:text="@string/labExercise"   
        android:layout_toLeftOf="@+id/inputExercise"
        android:textColor="#ffffff"/>

    <Button
        android:id="@+id/addbutton"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_margin="10dp"
        android:text="@string/add" />


    <Button
    android:id="@+id/startStop"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_above="@+id/addbutton" 
    android:layout_alignParentLeft="true"
    android:layout_margin="10dp"
    android:text="@string/startStop" />

    <TextView
        android:id="@+id/labWeight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/inputExercise"
        android:layout_margin="10dp"
        android:text="@string/labWeight" 
        android:textColor="#ffffff"/>

    <TextView
        android:id="@+id/labReps"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/inputReps"
        android:layout_margin="10dp"
        android:text="@string/labReps" 
        android:textColor="#ffffff"/>

    <TextView
        android:id="@+id/seePrevious"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/inputExercise"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="22dp"
        android:text="@string/tapToViewPrevious" 
        android:textColor="#505050"/>

</RelativeLayout>

and here is the code I used in my activity: (I have stripped out unneccesary code)

public class MyWorkoutDiary1Activity extends Activity implements OnClickListener, TextWatcher 
{

    TextView seePrevious;
    DatabaseHandler db;

    AutoCompleteTextView myAutoComplete;

    ArrayList<String> exerciseType = new ArrayList<String>();


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


        exercise = new EditText(this); 
        exercise = (EditText)findViewById(R.id.inputExercise); 

        db = new DatabaseHandler(this);
        exerciseType = db.getUniqueExercises();

        myAutoComplete = (AutoCompleteTextView)findViewById(R.id.inputExercise);

        myAutoComplete.addTextChangedListener(this);
        myAutoComplete.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, exerciseType));


        weight = new EditText(this); 
        weight = (EditText)findViewById(R.id.inputWeight); 


        reps = new EditText(this); 
        reps = (EditText)findViewById(R.id.inputReps); 

    }

}

Thanks for reading

回答1:

It works for me may be helpful to others as well, open your manifest file and set hardwareAccelerated="true".

 <application
    android:allowBackup="true"
    .....
    android:hardwareAccelerated="true">

For more about HardwareAccelerated https://developer.android.com/guide/topics/graphics/hardware-accel.html



回答2:

In fact your text is being typped, but that's a little bug that makes your text color be the same as your background, so you don't see it. This can be easily fixed by doing 2 things:

1) Simply change the textColor of your EditText, either defining it in the layout:

android:textColor="..."

or dynamically:

EditText et = (EditText) findViewById(R.id.your_edittext);
et.setTextColor(Color.RED);

2) Change the extended theme in your manifest:

<application android:theme="@style/Theme.Light.NoTitleBar.Workaround" ... >

3) Create the new theme at res/values/themes.xml which uses fixed styles:

<style name="Theme.Light.NoTitleBar.Workaround" parent="@android:style/Theme.Light.NoTitleBar">
  <item name="android:autoCompleteTextViewStyle">@style/AutoCompleteTextViewLight</item>
  <item name="android:dropDownItemStyle">@style/Widget.DropDownItemLight</item>
</style>

4) Now these styles, located at res/values/styles.xml should fix the color:

<style name="AutoCompleteTextViewLight" parent="@android:style/Widget.AutoCompleteTextView">
  <item name="android:textColor">@android:color/primary_text_light</item>
</style>
<style name="Widget.DropDownItemLight" parent="@android:style/Widget.DropDownItem">
  <item name="android:textColor">@android:color/primary_text_light</item>
</style>

I know it's a mess, but try it and if it works, try finding a combination of those attributes that fit to your layout.



回答3:

Ok, so I worked out what the issue was!

The code which I didn't post because I thought it was 'irrelevant' contained a thread

public static Runnable updateTimerMethod = new Runnable() 
{

    public void run() 
    {
        sessionTimer.setText(TimerHandler.theTime);  

        myHandler.postDelayed(this, 0);

    }
 }; 

I realised that the thread was basically taking up all of the activity (I don't really know how to explain that properly) by having the postDelayed as 0. Once I changed this to say... 100, then the EditText worked.

Thank you to @NKN who helped me.

This may be specific to me, but hopefully this will help somebody else.