欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现底部弹出的对话框功能

程序员文章站 2023-11-12 18:02:52
环境: 主机:win10 开发环境:android studio 2.2 preview 3 说明: 两种方法实现底部弹出的对话框: di...

环境:

  1. 主机:win10
  2. 开发环境:android studio 2.2 preview 3

说明:
两种方法实现底部弹出的对话框:

  1. dialog
  2. dialogfragment

推荐用dialogfragment

效果图:

Android实现底部弹出的对话框功能

布局文件dialog_select_call.xml:

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:orientation="vertical" android:layout_width="match_parent" 
  android:layout_height="match_parent"> 
 
  <relativelayout 
    android:id="@+id/layout_voice" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/white"> 
 
    <textview 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignparentstart="true" 
      android:layout_centerinparent="true" 
      android:textsize="16sp" 
      android:textcolor="@color/black" 
      android:layout_marginleft="16dp" 
      android:layout_marginright="16dp" 
      android:layout_margintop="16dp" 
      android:layout_marginbottom="16dp" 
      android:text="语音课堂"/> 
  </relativelayout> 
 
  <view 
    android:layout_width="fill_parent" 
    android:layout_height="0.1dp" 
    android:background="#b4b4b4" 
    android:layout_marginleft="16dp" 
    android:layout_marginright="16dp"/> 
 
  <relativelayout 
    android:id="@+id/layout_video" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/white"> 
 
    <textview 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignparentstart="true" 
      android:layout_centerinparent="true" 
      android:textsize="16sp" 
      android:textcolor="@color/black" 
      android:layout_marginleft="16dp" 
      android:layout_marginright="16dp" 
      android:layout_margintop="16dp" 
      android:layout_marginbottom="16dp" 
      android:text="视频课堂"/> 
  </relativelayout> 
 
  <view 
    android:layout_width="fill_parent" 
    android:layout_height="0.1dp" 
    android:background="#b4b4b4" 
    android:layout_marginleft="16dp" 
    android:layout_marginright="16dp"/> 
 
  <relativelayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/white"> 
 
    <button 
      android:id="@+id/cancel" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="取消" 
      android:layout_marginleft="16dp" 
      android:layout_marginright="16dp" 
      android:layout_margintop="16dp" 
      android:layout_marginbottom="16dp"/> 
  </relativelayout> 
 
</linearlayout> 

dialog实现源码:

初始化:

private void dialogselectcallinit() { 
    dialogselectcall = new dialog(this, r.style.dialogpopbottom); 
    view inflate = layoutinflater.from(this).inflate(r.layout.dialog_select_call, null); 
    dialogselectcall.setcontentview(inflate); 
 
    window dialogwindow = dialogselectcall.getwindow(); 
    dialogwindow.setgravity(gravity.bottom); 
 
    windowmanager.layoutparams lp = dialogwindow.getattributes(); 
    lp.x = 0; 
    lp.y = 0; 
    lp.width = getresources().getdisplaymetrics().widthpixels; 
    dialogwindow.setattributes(lp); 
 
    relativelayout layoutvoice = (relativelayout) inflate.findviewbyid(r.id.layout_voice); 
    relativelayout layoutvideo = (relativelayout) inflate.findviewbyid(r.id.layout_video); 
    button buttoncancel = (button) inflate.findviewbyid(r.id.cancel); 
 
    rxview.clicks(layoutvoice) 
        .throttlefirst(2, timeunit.seconds) 
        .compose(this.binduntilevent(activityevent.destroy)) 
        .subscribe(v -> { 
//          dialogselectcall.cancel(); 
          voicesessionactivity.startactivitycallout(this, userid); 
        }); 
 
    rxview.clicks(layoutvideo) 
        .throttlefirst(2, timeunit.seconds) 
        .compose(this.binduntilevent(activityevent.destroy)) 
        .subscribe(v -> { 
//          dialogselectcall.cancel(); 
//          videosessionactivity.startactivitycallout(this, userid); 
        }); 
 
    rxview.clicks(buttoncancel) 
        .throttlefirst(2, timeunit.seconds) 
        .compose(this.binduntilevent(activityevent.destroy)) 
        .subscribe(v -> dialogselectcall.cancel()); 
 
    rxview.touches(layoutvoice, motionevent -> { 
      deallayouttouch(layoutvoice, motionevent); 
      return false; 
    }).compose(this.binduntilevent(activityevent.destroy)).subscribe(motionevent -> {}); 
 
    rxview.touches(layoutvideo, motionevent -> { 
      deallayouttouch(layoutvideo, motionevent); 
      return false; 
    }).compose(this.binduntilevent(activityevent.destroy)).subscribe(motionevent -> {}); 
  } 
 
  private void deallayouttouch(view v, motionevent event) { 
    switch (event.getaction()) { 
      case motionevent.action_down: 
        v.setbackgroundcolor(color.rgb(200, 200, 200)); 
        break; 
      case motionevent.action_up: 
        v.setbackgroundcolor(color.white); 
        break; 
    } 
  } 

显示对话框:

dialogselectcall.show() 

dialogfragment实现源码:

定义了一个类selectcalldialog.java继承dialogfragment

package com.bazhangkeji.classroom.common; 
 
import android.app.dialog; 
import android.app.fragmentmanager; 
import android.graphics.color; 
import android.os.bundle; 
import android.support.annotation.nonnull; 
import android.view.gravity; 
import android.view.layoutinflater; 
import android.view.motionevent; 
import android.view.view; 
import android.view.viewgroup; 
import android.view.window; 
import android.view.windowmanager; 
import android.widget.button; 
import android.widget.relativelayout; 
 
import com.bazhangkeji.classroom.r; 
import com.bazhangkeji.classroom.session.videosessionactivity; 
import com.bazhangkeji.classroom.session.voicesessionactivity; 
import com.jakewharton.rxbinding2.view.rxview; 
import com.trello.rxlifecycle2.android.fragmentevent; 
import com.trello.rxlifecycle2.components.rxdialogfragment; 
 
 
import java.util.concurrent.timeunit; 
 
public class selectcalldialog extends rxdialogfragment { 
  private dialog dialog; 
  private string userid; 
 
  /** 
   * 初始化.必须调用一次 
   * @param userid: 目标用户id 
   */ 
  public void init(string userid) { 
    this.userid = userid; 
  } 
 
  @nonnull 
  @override 
  public dialog oncreatedialog(bundle savedinstancestate) { 
    dialog = new dialog(getactivity(), r.style.dialogpopbottom); 
 
    view inflate = layoutinflater.from(getactivity()).inflate(r.layout.dialog_select_call, null); 
    dialog.setcontentview(inflate); 
    dialog.setcanceledontouchoutside(true); 
 
    window window = dialog.getwindow(); 
    windowmanager.layoutparams lp = window.getattributes(); 
    lp.gravity = gravity.bottom; 
    lp.width = windowmanager.layoutparams.match_parent; 
    window.setattributes(lp); 
 
    relativelayout layoutvoice = (relativelayout) inflate.findviewbyid(r.id.layout_voice); 
    relativelayout layoutvideo = (relativelayout) inflate.findviewbyid(r.id.layout_video); 
    button buttoncancel = (button) dialog.findviewbyid(r.id.cancel); 
 
    rxview.clicks(layoutvoice) 
        .throttlefirst(2, timeunit.seconds) 
        .compose(this.binduntilevent(fragmentevent.destroy)) 
        .subscribe(v -> { 
          dialog.cancel(); 
          voicesessionactivity.startactivitycallout(getactivity(), userid); 
        }); 
 
    rxview.clicks(layoutvideo) 
        .throttlefirst(2, timeunit.seconds) 
        .compose(this.binduntilevent(fragmentevent.destroy)) 
        .subscribe(v -> { 
          dialog.cancel(); 
          videosessionactivity.startactivitycallout(getactivity(), userid); 
        }); 
 
    rxview.clicks(buttoncancel) 
        .throttlefirst(2, timeunit.seconds) 
        .compose(this.binduntilevent(fragmentevent.destroy)) 
        .subscribe(v -> dialog.cancel()); 
 
    rxview.touches(layoutvoice, motionevent -> { 
      deallayouttouch(layoutvoice, motionevent); 
      return false; 
    }).compose(this.binduntilevent(fragmentevent.destroy)).subscribe(motionevent -> {}); 
 
    rxview.touches(layoutvideo, motionevent -> { 
      deallayouttouch(layoutvideo, motionevent); 
      return false; 
    }).compose(this.binduntilevent(fragmentevent.destroy)).subscribe(motionevent -> {}); 
 
    return dialog; 
  } 
 
  private void deallayouttouch(view v, motionevent event) { 
    switch (event.getaction()) { 
      case motionevent.action_down: 
        v.setbackgroundcolor(color.rgb(200, 200, 200)); 
        break; 
      case motionevent.action_up: 
        v.setbackgroundcolor(color.white); 
        break; 
    } 
  } 
} 

显示对话框前初始化参数:

selectcalldialog.init(userid); 

显示对话框后:

selectcalldialog.show(getfragmentmanager(), ""); 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。