手记

底部弹出 和输入法遮挡问题的解决

package com.example.dialog;

import android.os.Bundle;

import android.annotation.SuppressLint;

import android.app.Activity;

import android.app.Dialog;

import android.view.Display;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.Window;

import android.view.WindowManager;

import android.widget.Button;

import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    private View inflate;

    private Button choosePhoto;

    private Button takePhoto;

    private Button cancel;

    private Dialog dialog;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

    @SuppressWarnings("deprecation")

@SuppressLint("InflateParams")

public void show(View view){

        dialog = new Dialog(this);

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        inflate = LayoutInflater.from(this).inflate(R.layout.dialog, null);

        choosePhoto = (Button) inflate.findViewById(R.id.choosePhoto);

        takePhoto = (Button) inflate.findViewById(R.id.takePhoto);

       cancel = (Button) inflate.findViewById(R.id.btn_cancel);

        choosePhoto.setOnClickListener(this);

        takePhoto.setOnClickListener(this);

        cancel.setOnClickListener(this);

        dialog.setContentView(inflate);

        Window dialogWindow = dialog.getWindow();

        dialogWindow.setGravity( Gravity.BOTTOM);

//        应该是dialogWindow.getAttributes:

        WindowManager m = getWindowManager();

        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用

        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值

//        p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的0.6

        p.width = (int) (d.getWidth() * 1); // 宽度设置为屏幕的0.65

        dialogWindow.setAttributes(p);

        dialog.show();

    }

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

switch (v.getId()){

        case R.id.takePhoto:

            Toast.makeText(this,"点击了拍照",Toast.LENGTH_SHORT).show();

            break;

        case R.id.choosePhoto:

            Toast.makeText(this,"点击了选择照片",Toast.LENGTH_SHORT).show();

            break;

        case R.id.btn_cancel:

            dialog.dismiss();

    }

}

}

********************************************

<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" >

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_centerInParent="true"

            android:onClick="show"

            android:text="显示弹框"

            />

</RelativeLayout>

*****************************************

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <Button

        android:id="@+id/takePhoto"

        android:layout_width="match_parent"

        android:layout_height="45dp"

        android:gravity="center"

        android:text="拍照"

        android:textColor="#0000ff"

        android:textSize="18sp"

        android:textStyle="bold" />

    <Button

        android:id="@+id/choosePhoto"

        android:layout_width="match_parent"

        android:layout_height="45dp"

        android:gravity="center"

        android:text="从相册选择"

        android:textColor="#0000ff"

        android:textSize="18sp"

        android:textStyle="bold" />

    <Button

        android:id="@+id/btn_cancel"

        android:layout_width="match_parent"

        android:layout_height="45dp"

        android:layout_marginTop="8dp"

        android:gravity="center"

        android:text="取消"

        android:textColor="#0000ff"

        android:textSize="18sp"

        android:textStyle="bold" />

</LinearLayout>

////////////////////////////////////////////////原来的代码////////////////////////////////////////////////////////////////////////////

// //底部弹出 评论

// @SuppressWarnings("deprecation")

// public void dialog(){

//// detail_content.setVisibility(View.GONE);

// dialog = new Dialog(mContext,R.style.AppBaseTheme);

//// dialog.setCanceledOnTouchOutside(true);

// dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

//        inflate = LayoutInflater.from(mContext).inflate(R.layout.fragment_video_class_commit_dialog, null);

// commit = (TextView) inflate.findViewById(R.id.video_relpy_tv_reply_delete);

// reply_content=(EditText) inflate.findViewById(R.id.video_relpy_tv_reply_content1);

// rbRating_reply = (RatingBar) inflate.findViewById(R.id.video_relpy_rb_evaluation1);

// rbRating_reply.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

// @Override

// public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) {

// if (ratingBar.getId() == R.id.video_relpy_rb_evaluation1) {

// scoreContent = String.valueOf((int) rating);

// }

// }

// });

// commit.setOnClickListener(new OnClickListener() {

// @Override

// public void onClick(View v) {

// if (first) {

// cont=reply_content.getText().toString();

//  if (cont.equals("")) {

//

// DialogSingleButton alertDialog = new DialogSingleButton(

// mContext, "评论不能为空");

// alertDialog.show();

// return;

// }

// commitEvaluation(cont);

// first=false;

// }else {

// pg.cancel();

// DialogSingleButton alertDialog = new DialogSingleButton(

// mContext, "只能评论一次");

// alertDialog.show();

// return;

// }

//  

// }

// });

//        dialog.setContentView(inflate);

//        float aaaa = 0;

//        Window dialogWindow = dialog.getWindow();

//       

//        dialogWindow.setGravity( Gravity.BOTTOM);

//        WindowManager m = getActivity().getWindowManager();

//        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高

//        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值

//        p.height = (int) (d.getHeight() * 0.2); // 高度设置为屏幕的0.6

//        p.width = (int) (d.getWidth() * 1); // 宽度设置为屏幕的0.65

//        dialogWindow.setAttributes(p);

////        dialogWindow

////        dialogWindow.setLayout(500, 300);

//        dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); 

//        dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |

//                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 

//        dialog.show();

//  

//

// }

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

//遮挡问题的解决 fragment中

//底部弹出 评论

@SuppressWarnings("deprecation")

public void dialog(){

detail_content.setVisibility(View.GONE);

inflate = LayoutInflater.from(mContext).inflate(R.layout.fragment_video_class_commit_dialog, null);

commit = (TextView) inflate.findViewById(R.id.video_relpy_tv_reply_delete);

reply_content=(EditText) inflate.findViewById(R.id.video_relpy_tv_reply_content1);

reply_content.setOnFocusChangeListener(new OnFocusChangeListener() {

@Override

public void onFocusChange(View v, boolean hasFocus) {

if (hasFocus) { 

detial_favorite.setVisibility(View.GONE);

}else

{

detial_favorite.setVisibility(View.VISIBLE);

}

}

});

rbRating_reply = (RatingBar) inflate.findViewById(R.id.video_relpy_rb_evaluation1);

rbRating_reply.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

@Override

public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) {

if (ratingBar.getId() == R.id.video_relpy_rb_evaluation1) {

scoreContent = String.valueOf((int) rating);

}

}

});

commit.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

if (first) {

cont=reply_content.getText().toString();

 if (cont.equals("")) {

DialogSingleButton alertDialog = new DialogSingleButton(

mContext, "评论不能为空");

alertDialog.show();

return;

}

commitEvaluation(cont);

first=false;

}else {

pg.cancel();

DialogSingleButton alertDialog = new DialogSingleButton(

mContext, "只能评论一次");

alertDialog.show();

return;

}

 

}

});

 WindowManager m = getActivity().getWindowManager();

 Display d = m.getDefaultDisplay(); // 获取屏幕宽、高

 int h=(int)Math.round(d.getHeight() * 0.2);

//  WindowManager.LayoutParams.FILL_PARENT,WindowManager.LayoutParams.WRAP_CONTENT,false);

  final PopupWindow popWindow = new PopupWindow(inflate,d.getWidth() * 1,h);  

        popWindow.setAnimationStyle(R.style.AppBaseTheme);  

        popWindow.setFocusable(true);  

        popWindow.setOutsideTouchable(true);// 设置允许在外点击消失  

        //这句是为了防止弹出菜单获取焦点之后,点击activity的其他组件没有响应

        popWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);

//防止虚拟软键盘被弹出菜单遮住

        popWindow.setBackgroundDrawable(new BitmapDrawable()); 

        

        popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        popWindow.showAtLocation(super.getView(), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);  

      

}

原文链接:http://www.apkbus.com/blog-851511-61945.html

0人推荐
随时随地看视频
慕课网APP