gpt4 book ai didi

android - 使编辑文本出现在android中设备的顶部和软键盘之间

转载 作者:行者123 更新时间:2023-11-29 02:03:06 24 4
gpt4 key购买 nike

我有一个包含许多 ViewsStub 的 ScrollView 布局,这些 View stub 会根据用户操作膨胀(显示),例如......回答第一个问题会使第二个问答字段(editText)出现

我的问题是在回答完第一个问题并点击提交后,下一个问题可见,但答案字段隐藏在软键盘下...用户看不到他正在输入的内容。

我想要的是任何新显示的 editText/spinner/checkbox 必须出现在屏幕顶部(动态滚动到这些 View )。

如何实现??

最佳答案

以我的代码为例,您可以执行以下操作:将 XML 设为:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ScrollView
android:id="@+id/scrlv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#123789" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#1188ff" >

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/headerlayout"
android:background="#456789" >

<EditText
android:id="@+id/etemail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp"
android:hint="Text 1"
android:maxLines="3" />

<EditText
android:id="@+id/etpassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etemail"
android:layout_alignRight="@+id/etemail"
android:layout_below="@+id/etemail"
android:hint="Text 2"
android:inputType="textPassword"
android:maxLines="3" />

<EditText
android:id="@+id/etusername"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etemail"
android:layout_alignRight="@+id/etemail"
android:layout_below="@+id/etpassword"
android:hint="Text 3"
android:maxLines="3" />

<EditText
android:id="@+id/etphone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etemail"
android:layout_alignRight="@+id/etemail"
android:layout_below="@+id/etusername"
android:hint="Text 4"
android:inputType="text"
android:maxLines="5" />

<CheckBox
android:id="@+id/chkterms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etemail"
android:layout_alignRight="@+id/etemail"
android:layout_below="@+id/etphone"
android:layout_marginTop="10dp"
android:text="Conditions Aplly"
android:textColor="@android:color/black" />

<Button
android:id="@+id/btnlogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/chkterms"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="Login" />
</RelativeLayout>
</LinearLayout>
</ScrollView>

</RelativeLayout>

然后代码:

[1] 获取设备的屏幕分辨率:高度、宽度

[2] 对 EditText 进行 onFocusChange

[3] 这样,如果您的 EditText 获得焦点,那么

[4] 获取 EditText 的 Bottom,如果 Bottom 大于 (ScreenHeight/3) 则

[5] 将 Your_ScrollView 滚动到 (left, (ScreenHeight/3));

public class MainActivity extends Activity implements OnClickListener{
private EditText etEmail, etPassword, etUserName, etPhoneNo;
private CheckBox chkTerms;
private Button btnLogin;
ScrollView scrlv;

int sw, sh, left, bottom;

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

scrlv = (ScrollView) findViewById(R.id.scrlv);

etEmail = (EditText) findViewById(R.id.etemail);
etPassword = (EditText) findViewById(R.id.etpassword);
etUserName = (EditText) findViewById(R.id.etusername);
etPhoneNo = (EditText) findViewById(R.id.etphone);

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
sw = dm.widthPixels;
sh = dm.heightPixels;
System.out.println("Screen Width = " + sw);
System.out.println("Screen Height = " + sh);

chkTerms = (CheckBox) findViewById(R.id.chkterms);

btnLogin = (Button) findViewById(R.id.btnlogin);
btnLogin.setOnClickListener(this);

etUserName.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(etUserName.hasFocus()) {
left = etUserName.getLeft();
bottom = etUserName.getTop();
if (bottom > sh / 3) {
System.out.println("Umn Left :: " + left);
System.out.println("Umn Bottom :: " + bottom);
scrlv.scrollTo(left, (sh/3));
left = etUserName.getLeft();
bottom = etUserName.getTop();
System.out.println("Umn Left :: " + left);
System.out.println("Unm Bottom :: " + bottom);
}
}
}
});

etPhoneNo.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(etPhoneNo.hasFocus()) {
left = etPhoneNo.getLeft();
bottom = etPhoneNo.getTop();
if (bottom > sh / 3) {
System.out.println("Phno Left :: " + left);
System.out.println("Phno Bottom :: " + bottom);
scrlv.scrollTo(0, (sh/3));
left = etPhoneNo.getLeft();
bottom = etPhoneNo.getTop();
System.out.println("Phno Left :: " + left);
System.out.println("Phno Bottom :: " + bottom);
}
}
}
});

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}
}

关于android - 使编辑文本出现在android中设备的顶部和软键盘之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11541862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com