我想检查屏幕上是否可见软键盘。为此,我找到了一个代码并按照给定的方式尝试了它。但两者都 activityRootView.getRootView().getHeight()
返回activityRootView.getHeight()
零。我需要你的专家意见。
代码:
LayoutInflater inflator5=
(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row5 =
inflator5.inflate(R.layout.order_confirmation_list2,null);
final LinearLayout activityRootView = (LinearLayout)row5.
findViewById(R.id.orderconfirmRootId);
// calculate the difference between the root view height and the window view height
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
// if more than 100 pixels, its probably a keyboard...
if (heightDiff > 100) {
// keyboard is visible, do something here
Toast toast = Toast.makeText(context, "keyboard visible!!!" , Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
} else {
// keyboard is not visible, do something here
}
更新这个问题:
我遵循了许多与此类型相关的答案,但没有找到合适的答案。请浏览我新添加的代码。
当软键盘可见和隐藏时,如何在 getview() 方法中获取布局的高度差异?我在下面给出的代码中尝试了这个 ((EditText) view.findViewById(R.id.editTextQtyId)).addTextChangedListener。但总是通过 getHeight() 方法返回零。
随时欢迎您的专家观点。
新代码:
@Override
public View getView(int i, final View view, final ViewGroup viewgroup) {
row=view;
holder=null;
try
{
if(row == null)
{
Button RemoveOrderBtn;
LayoutInflater inflator=
(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row=inflator.inflate(R.layout.add_to_orderlist2,viewgroup , false);
holder=new MyViewHolder(row);
row.setTag(holder);
Log.d("newrows", "new row");
}
else
{
holder= (MyViewHolder) row.getTag();
Log.d("recycling", "Recycling stuff");
}
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
SingleRow temp=list.get(i);
holder.Dno.setText(temp.Dno);
holder.Size.setText(temp.size);
holder.Mrp.setText(temp.Mrp);
holder.Color.setText(temp.color);
holder.SingleOrderTotal.setText(temp.val_total);
holder.P_Category.setText(temp.Pcategory);
holder.editTextQtyId.setText(temp.val_count);
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard.getAbsolutePath() + "/App" +temp.imageName);
FileInputStream streamIn;
try {
streamIn = new FileInputStream(file);
Bitmap bitmap = BitmapFactory.decodeStream(streamIn);
holder.Image.setImageBitmap(bitmap);
streamIn.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
if(view!=null)
{
((EditText) view.findViewById(R.id.editTextQtyId)).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
((EditText) view.findViewById(R.id.editTextQtyId)).addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
LayoutInflater inflator5=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row5 = inflator5.inflate(R.layout.order_confirmation_list2,null);
final LinearLayout layout = (LinearLayout)row5. findViewById(R.id.orderconfirmRootId);
Log.d("Log", "Height: " + layout.getHeight());
Toast toast = Toast.makeText(context, "Height= "+layout.getHeight() , Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
});
return false;
}
});
}
}
catch(Exception e)
{
e.printStackTrace();
}
return row;
}