简体   繁体   English

android中显示软键盘时,edittext在错误的位置

[英]edittext are in the wrong place when softkeyboard are shown in android

i face a difficulty to resolve a problem on my android application. 我在解决Android应用程序上的问题时遇到困难。 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 问题是,当用户单击textedit时,它没有使文本编辑高出键盘上方,而是将edittext像这样卡在软键盘的中间顶部 在此处输入图片说明

why is this happening? 为什么会这样呢? how can i move the text edit a little above the keyboard. 我如何将文本编辑移到键盘上方一点。

here is the java code 这是java代码

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 这是xml代码

<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" windowSoftInputMode = “adjustResize”

OR 要么

Make changes in your onCreate()method in the activity class like 在活动类中的onCreate()方法中进行更改,例如

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

Make the code change for the activity declaration in the AndroidManifest.xml 在AndroidManifest.xml中更改活动声明的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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