java - 当特定的 EditText 具有焦点时滚动整个页面?

标签 java android android-edittext scrollview android-linearlayout

我有注册页面,因为存在四个 EditText,当我单击第三个 EditText 时,一个 LinearLayout(假设密码指南)必须可见,同时应滚动总布局以查看所有指南。当其他 EditText ( 1,2 或 4) 得到焦点应该关闭此 LinearLayout(密码指南)。我试了很多,我搜索了很多,没有适合我的解决方案,请帮助我。为了了解我做了一个小项目,请观看。

public class MainActivity extends AppCompatActivity {
private EditText mEditText1,mEditText2,mEditText3,mEditText4;
private LinearLayout ll,root;
ScrollView mScrollView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    root = (LinearLayout) findViewById(R.id.ll_linear_layout);
    ll = (LinearLayout) findViewById(R.id.ll);
    mEditText1 = (EditText) findViewById(R.id.et1);
    mEditText2 = (EditText) findViewById(R.id.et2);
    mEditText3 = (EditText) findViewById(R.id.et3);
    mEditText4 = (EditText) findViewById(R.id.et4);

    mEditText1.setOnFocusChangeListener(mListener1);
    mEditText2.setOnFocusChangeListener(mListener2);
    mEditText3.setOnFocusChangeListener(mLayoutExtract);
    mEditText4.setOnFocusChangeListener(mListener4);


    mScrollView = new ScrollView(this);

}

View.OnFocusChangeListener mListener1 = new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean b) {
        if (b) {

        } else {

        }

    }
};
View.OnFocusChangeListener mListener2 = new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean b) {
        if (b) {

        } else {

        }

    }
};
View.OnFocusChangeListener mLayoutExtract = new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean b) {
        if (b) {
            ll.setVisibility(View.VISIBLE);
        } else {
            ll.setVisibility(View.GONE);
        }

    }
};
View.OnFocusChangeListener mListener4 = new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean b) {
        if (b) {


        } else {

        }

    }
}};


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/ll_linear_layout"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
    android:layout_marginTop="200dp"
    android:id="@+id/et1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="first name"/>
<EditText
    android:id="@+id/et2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="second name"/>
<EditText
    android:id="@+id/et3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="second name"/>
    <LinearLayout
        android:id="@+id/ll"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:visibility="gone">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="75dp"
            android:textColor="#000000"
            android:text="Hidable text"/>
    </LinearLayout>
<EditText
    android:id="@+id/et4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="second name"/>

最佳答案

试试这个

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/ll_linear_layout"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
    android:layout_marginTop="200dp"
    android:id="@+id/et1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="first name"/>
<EditText
    android:id="@+id/et2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="second name"/>
<EditText
    android:id="@+id/et3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="second name"/>
    <LinearLayout
        android:id="@+id/ll"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:visibility="gone">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="75dp"
            android:textColor="#000000"
            android:text="Hidable text"/>
    </LinearLayout>
<EditText
    android:id="@+id/et4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="second name"/>
</LinearLayout>
</ScrollView>

试试这个在你的 list 中使用 android:windowSoftInputMode="AdjustResize"你正在 decraing YOur Activity 就像下面的代码一样

<activity
    android:name=".YourActivity"
    android:parentActivityName="XXX.XXX.XXXX"
    android:windowSoftInputMode="AdjustResize" />

关于java - 当特定的 EditText 具有焦点时滚动整个页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47097177/

相关文章:

java - 使用 EditText 编辑 RecyclerView 中的重复项目

android - EditText 的 setText() 在 onTextChanged 方法中不起作用

android - 从 EditText 获取值时出错?

java - 引用 JPanel Action 监听器内部的类

java - 我是否需要在保证发生之前的对象上进行同步?

Android eclipse 模拟器闪白屏

java - 点后显示最多 X 位数字

android - 我如何编写其他应用程序可读的文件?

java - 如何在触发器的帮助下更新不同 sql 表中的行

java - 为数据库类创建通用 "getColumn"方法 (Java)