如何防止编辑文本在键盘打开时缩小?

huangapple go评论76阅读模式
英文:

how to prevent edit text from shrinking when keyboard opens?

问题

以下是您要翻译的内容:

I am a beginner in Android, I am trying to make a simple app where text in the edit text gets added to recycler view, but as soon as the Keyboard opens, edit text shrinks and content inside it is not visible

This is activty_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    tools:context=".MainActivity">

   <LinearLayout
       android:layout_weight="1"
       android:layout_width="match_parent"
       android:layout_height="0dp" >

      <EditText
          android:hint="ADD ITEMS"
          android:layout_margin="10dp"
          android:id="@+id/etItems"
          android:textSize="24sp"
          android:textColor="@android:color/black"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="3" />

      <Button
          android:layout_margin="10dp"
          android:id="@+id/btnAdd"
          android:text="ADD"
          android:layout_weight="1"
          android:autoSizeTextType="uniform"
          android:layout_width="0dp"
          android:layout_height="match_parent" />
   </LinearLayout>

   <androidx.recyclerview.widget.RecyclerView
       android:id="@+id/rvItems"
       android:layout_margin="10dp"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="6" />

</LinearLayout>

This is MainActivity.java

package com.example.todolist_09082020;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ArrayList<String> items;
    Button btnAdd;
    EditText etItems;
    RecyclerView rvItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnAdd = findViewById(R.id.btnAdd);
        etItems = findViewById(R.id.etItems);
        rvItems = findViewById(R.id.rvItems);

        items = new ArrayList<>();

        final ItemAdapter adapter = new ItemAdapter(items);
        rvItems.setLayoutManager(new LinearLayoutManager(this));

        rvItems.setAdapter(adapter);

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = etItems.getText().toString();
                if(!text.equals("")){
                    items.add(text);
                    adapter.notifyDataSetChanged();
                }
                etItems.setText("");
            }
        });
    }
}

I tried solutions from other posts related to this, but this could not be solved.

edit text shrinked

英文:

I am a beginner in Android, I am trying to make a simple app where text in the edit text gets added to recycler view, but as soon as the Keyboard opens, edit text shrinks and content inside it is not visible

This is activty_main.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;
tools:context=&quot;.MainActivity&quot;&gt;
&lt;LinearLayout
android:layout_weight=&quot;1&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;0dp&quot; &gt;
&lt;EditText
android:hint=&quot;ADD ITEMS&quot;
android:layout_margin=&quot;10dp&quot;
android:id=&quot;@+id/etItems&quot;
android:textSize=&quot;24sp&quot;
android:textColor=&quot;@android:color/black&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_weight=&quot;3&quot; /&gt;
&lt;Button
android:layout_margin=&quot;10dp&quot;
android:id=&quot;@+id/btnAdd&quot;
android:text=&quot;ADD&quot;
android:layout_weight=&quot;1&quot;
android:autoSizeTextType=&quot;uniform&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;match_parent&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/rvItems&quot;
android:layout_margin=&quot;10dp&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;0dp&quot;
android:layout_weight=&quot;6&quot; /&gt;
&lt;/LinearLayout&gt;

This is MainActivity.Java

package com.example.todolist_09082020;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList&lt;String&gt; items;
Button btnAdd;
EditText etItems;
RecyclerView rvItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = findViewById(R.id.btnAdd);
etItems = findViewById(R.id.etItems);
rvItems = findViewById(R.id.rvItems);
items = new ArrayList&lt;&gt;();
final ItemAdapter adapter = new ItemAdapter(items);
rvItems.setLayoutManager(new LinearLayoutManager(this));
rvItems.setAdapter(adapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = etItems.getText().toString();
if(!text.equals(&quot;&quot;)){
items.add(text);
adapter.notifyDataSetChanged();
}
etItems.setText(&quot;&quot;);
}
});
}
}

I tried solutions from other posts related to this, but this could not be solved.

edit text shrinked

答案1

得分: 0

将EditText和其父LinearLayout的layout_height更改为wrap_content。这应该会解决您的问题。

英文:

Change layout_height of EditText and its parent LinearLayout to wrap_content. It should solve your issue

答案2

得分: 0

你的问题是线性布局中文本和按钮高度的设置。有几种方法可以解决,但保持它不会折叠的最简单方式是将

android:layout_height="0dp"

改为

android:layout_height="wrap_content"

如果你还将文本和按钮的高度改为wrap_content,并为按钮文本选择一个固定的字体大小(例如24sp),可能会看到更好的效果。

英文:

Your problem is the linear layout holding the text and button height. There's several things you can do, but a minimal way to keep it from collapsing is to change

android:layout_height=&quot;0dp&quot;

to

android:layout_height=&quot;wrap_content&quot;

You'll probably also see nicer results if you additionally change the text and button heights to wrap_content and choose a fixed font size (24sp looked nice) for your button text.

huangapple
  • 本文由 发表于 2020年8月9日 19:21:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63325698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定