简体   繁体   中英

edittext are in the wrong place when softkeyboard are shown in android

i face a difficulty to resolve a problem on my android application. the problem is when the user click on textedit is that it did not brought the text edit high above the keyboard, instead the edittext are stuck in the middle top of the softkeyboard like this 在此处输入图片说明

why is this happening? how can i move the text edit a little above the keyboard.

here is the java code

public class MainActivity extends AppCompatActivity {
    ListView listView;
    ImageButton micBtn, keyBtn;
    EditText textcontent;
    Json jsons;
    Files files;
    ArrayList<String> names;
    ArrayList<Integer> id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );

        jsons = new Json( this );
        files = new Files( this );

        listView = (ListView) findViewById( R.id.textListView );
        micBtn = (ImageButton) findViewById( R.id.micBtn );
        keyBtn = (ImageButton) findViewById( R.id.keyBtn );
        textcontent = (EditText) findViewById( R.id.textContent );

        //final String[] names = {"hello","good","hello","good","hello","good","hello","good","hello","good","hello","good","hello","good"};
        //int[] id = {0,1,0,1,0,1,0,1,0,1,0,0,1,1};
        names = new ArrayList<>(  );
        id = new ArrayList<>(  );

        final TextListView baseAdapter = new TextListView(MainActivity.this,names,id);
        listView.setAdapter( baseAdapter );
        listView.setSelection( baseAdapter.getCount() - 1 );

        textcontent.setOnFocusChangeListener( new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {

            }
        } );

        micBtn.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        } );

        keyBtn.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content = String.valueOf( textcontent.getText() );
                if(content != ""){
                    names.add( content );
                    id.add( 0 );
                    String answer = jsons.sendText( "are you ok" );
                    answering( answer );
                    listView.setSelection( baseAdapter.getCount() - 1 );
                    textcontent.setText( "" );
                }else{
                    Toast.makeText( MainActivity.this,"what are your command?",Toast.LENGTH_LONG ).show();
                }

            }
        } );
    }

    public void answering(String answer){
        if(answer != "") {
            names.add( answer );
            id.add( 1 );
        }else{

        }
    }
}

and here is the xml code

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:background="@color/background">

    <ListView
        android:id="@+id/textListView"
        android:layout_weight="0.9"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:divider="@null"
        android:dividerHeight="0dp">

    </ListView>

    <LinearLayout
        android:layout_weight="0.1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:background="@color/bottombar">

        <ImageButton
            android:id="@+id/micBtn"
            android:layout_marginLeft="10dp"
            android:layout_gravity="center"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@color/userCircle"/>

        <EditText
            android:id="@+id/textContent"
            android:layout_marginTop="10dp"
            android:layout_weight="0.5"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:paddingLeft="10dp"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:background="@drawable/roundecorner"/>

        <ImageButton
            android:id="@+id/keyBtn"
            android:layout_gravity="center"
            android:layout_marginRight="10dp"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@color/userCircle"/>

    </LinearLayout>
</LinearLayout>

Make changes in the activity of your Manifest file like

windowSoftInputMode="adjustResize"

OR

Make changes in your onCreate()method in the activity class like

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

Make the code change for the activity declaration in the AndroidManifest.xml

  <activity
      android:name=".MainActivity "
      android:windowSoftInputMode="adjustResize" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM