android编辑框被软键盘覆盖
「已注销」 2019-06-05 12:00:46 我写了一个意见反馈的程序(还没加入服务器。。)我有两个文本编辑框,但是在编辑的时候都会被然键盘覆盖住
在网上找了方法在layout加入android:windowSoftInputMode="stateVisible|adjustResize"和
在oncreat加getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);都不行
求大佬相助(代码如下)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan">
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户反馈"
android:layout_centerHorizontal="true"
android:textSize="25dp"/>
<TextView
android:id="@+id/TextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择下列您觉得有问题的选项"
android:gravity="left"
android:layout_below="@id/TextView1"
android:textSize="20dp"/>
<CheckBox
android:id="@+id/CheckBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="页面设计不美观"
android:checked="false"
android:layout_below="@id/TextView2"
android:layout_alignParentLeft="true"/>
<CheckBox
android:id="@+id/CheckBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="产品功能不全"
android:checked="false"
android:layout_below="@id/CheckBox1"
android:layout_alignParentLeft="true"/>
<CheckBox
android:id="@+id/CheckBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="产品优化不足或存在漏洞"
android:checked="false"
android:layout_below="@id/CheckBox2"
android:layout_alignParentLeft="true"/>
<CheckBox
android:id="@+id/CheckBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="广告太多"
android:checked="false"
android:layout_below="@id/CheckBox3"
android:layout_alignParentLeft="true"/>
<TextView
android:id="@+id/TextView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您遇到的具体问题是什么?"
android:layout_below="@id/CheckBox4"
android:layout_alignParentLeft="true"
android:textSize="20dp"/>
<EditText
android:id="@+id/EditText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="200dp"
android:background="@drawable/shape"
android:gravity="top"
android:hint="请输入您的问题(没有可不填)"
android:editable="true"
android:layout_below="@id/TextView3"/>
<TextView
android:id="@+id/TextView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/EditText1"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:paddingBottom="5dp"
android:paddingRight="15dp"
android:textSize="10dp"/>
<TextView
android:id="@+id/TextView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您的联系方式是什么?"
android:layout_below="@id/EditText1"
android:textSize="20dp"/>
<EditText
android:id="@+id/EditText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape"
android:gravity="top"
android:hint="请留下邮箱或电话(没有可不填)"
android:editable="true"
android:layout_below="@id/TextView5"
android:layout_alignParentLeft="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
android:id="@+id/Button"
android:layout_below="@id/EditText2"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
package com.example.imagebutton;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Toast;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
//设置4个复选框
private CheckBox checkbox1,checkbox2,checkbox3,checkbox4;
//设置两个编辑框
private EditText editText1;
private EditText edittext2;
//设置编辑框1的字数限制
private TextView textView;
//编辑框1的最大字数为400
private int num=400;
//设置提交按钮
private Button button;
//将复选框放入容器中
private List<CheckBox> checkBoxList = new ArrayList<CheckBox>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
//初始化四个复选框
checkbox1 = (CheckBox) findViewById(R.id.CheckBox1);
checkbox2 = (CheckBox) findViewById(R.id.CheckBox2);
checkbox3 = (CheckBox) findViewById(R.id.CheckBox3);
checkbox4 = (CheckBox) findViewById(R.id.CheckBox4);
//添加复选框到容器中
checkBoxList.add(checkbox1);
checkBoxList.add(checkbox2);
checkBoxList.add(checkbox3);
checkBoxList.add(checkbox4);
//初始化按钮
button=(Button)findViewById(R.id.Button);
//初始化编辑框1的字数限制
textView=(TextView)findViewById(R.id.TextView4);
textView.setText(num+"");
//初始化编辑框1
editText1=(EditText)findViewById(R.id.EditText1);
//设置提交按钮监听事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer sb = new StringBuffer();
//遍历集合中的checkBox,判断是否选择,获取选中的文本
for(CheckBox checkbox:checkBoxList) {
if (checkbox.isChecked()){
sb.append(checkbox.getText().toString()+ " ");
}
}
if(sb!=null && "".equals(sb.toString())){
Toast.makeText(getApplicationContext(), "请至少选择一个", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), sb.toString(), Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this,"提交成功!",Toast.LENGTH_LONG).show();
}
}
});
editText1.addTextChangedListener(new TextWatcher() {
private CharSequence temp;
private int selectionStart;
private int selectionEnd;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
temp=s;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
public void afterTextChanged(Editable s) {
int number=num-s.length();
textView.setText(""+number);
selectionStart=editText1.getSelectionStart();
selectionEnd=editText1.getSelectionEnd();
if (temp.length()>num) {
s.delete(selectionStart-1, selectionEnd);
int tempSelection=selectionEnd;
editText1.setText(s);
editText1.setSelection(tempSelection);//设置光标在最后
Toast.makeText(MainActivity.this, "字数达到上限", Toast.LENGTH_SHORT).show();
}
}
});
}
}