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

时间:2018-12-12 15:59:54

标签: java android

我在解决Android应用程序上的问题时遇到了困难。问题是,当用户单击textedit时,它没有将文本编辑带到键盘上方,而是将edittext像这样enter image description here

固定在软键盘的中间顶部

为什么会这样?我如何将文本编辑移到键盘上方一点。

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

2 个答案:

答案 0 :(得分:0)

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

windowSoftInputMode =“ adjustResize”

OR

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

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

答案 1 :(得分:0)

在AndroidManifest.xml中更改活动声明的代码

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