首页 > 解决方案 > 当软键盘在android中显示时edittext在错误的位置

问题描述

我在解决我的 android 应用程序上的问题时遇到了困难。问题是当用户单击 textedit 时,它没有将文本编辑带到键盘上方,而是像这样将 edittext 卡在软键盘的中间顶部在此处输入图像描述

为什么会这样?我怎样才能将文本编辑移动到键盘上方一点。

这是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{

        }
    }
}

这是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>

标签: javaandroid

解决方案


更改清单文件的活动,例如

windowSoftInputMode="adjustResize"

或者

在活动类中更改 onCreate() 方法,例如

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


推荐阅读