android tv 自定义频道,安卓TV自定义dialog实现

安卓TV自定义dialog实现

安卓TV自定义dialog实现

1,继承自dialog

public class DialogChoice02 extends Dialog

2,重写构造方法,自定义style(定义dialog弹窗动画,背景等)

public DialogChoice02(@NonNull Context context) {

super(context, R.style.DialogScaleStyle);

}

style:

@android:color/transparent

@android:color/transparent

@style/MyDialogAnimStyle

true

true

dialog 显示消失动画

@anim/dialog_in

@anim/dialog_out

anmi:in

android:pivotX="50%"

android:pivotY="50%"

android:fromXScale="0"

android:fromYScale="0"

android:toXScale="1"

android:toYScale="1"

android:duration="300"/>

android:duration="300"

android:fromAlpha="0"

android:toAlpha="1"

/>

anmi:out

android:fromXScale="1"

android:fromYScale="1"

android:pivotX="50%"

android:pivotY="50%"

android:toXScale="0"

android:toYScale="0"

android:duration="200"/>

android:duration="200"

android:fromAlpha="1"

android:toAlpha="0"

/>

3,重写onCreate 在dialog创建时只执行一次,,如果要实时刷新dialog信息,另写更新UI的方法

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.dialog_choice);

//按空白处不能取消动画

setCanceledOnTouchOutside(false);

//初始化界面控件

initView();

//初始化界面数据

initData();//dialog创建时只会执行一次

//初始化界面控件的事件

initEvent();

}

private void initView() {

title = (TextView) findViewById(R.id.title);

message = (TextView) findViewById(R.id.message);

positiveButton = (Button) findViewById(R.id.positiveButton);

negativeButton = (Button) findViewById(R.id.negativeButton);

}

private void initData() {

//如果用户自定了title和message

if (titleStr != null) {

title.setVisibility(View.VISIBLE);

title.setText(titleStr);

}

if (messageStr != null) {

message.setVisibility(View.VISIBLE);

message.setText(messageStr);

}

//如果设置按钮的文字

if (yesStr != null) {

positiveButton.setText(yesStr);

}

if (noStr != null) {

negativeButton.setText(noStr);

}

}

private void initEvent() {

//默认前面一个选中焦点,最好在外面再调用初始化焦点

positiveButton.requestFocus();

positiveButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (yesOnclickListener != null) {

yesOnclickListener.onYesClick();

}

dismiss();

}

});

negativeButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (noOnclickListener != null) {

noOnclickListener.onNoClick();

}

dismiss();

}

});

}

/**

* 从外界Activity为Dialog设置标题

*

* @param titleText

*/

public void setTitle(String titleText) {

titleStr = titleText;

if (title != null && titleStr != null) {

title.setVisibility(View.VISIBLE);

title.setText(titleStr);

}

}

/**

* 从外界Activity为Dialog设置dialog的message

*

* @param messageText

*/

public void setMessage(String messageText) {

messageStr = messageText;

if (message != null && messageStr != null) {

message.setVisibility(View.VISIBLE);

message.setText(messageStr);

}

}

4,设置确定按钮和取消被点击的接口

public interface onYesOnclickListener {

void onYesClick();

}

public interface onNoOnclickListener {

void onNoClick();

}

/**

* 设置取消按钮的显示内容和监听

* @param str

* @param onNoOnclickListener

*/

public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {

if (str != null) {

noStr = str;

}

if (noStr != null && negativeButton != null) {

negativeButton.setText(noStr);

}

this.noOnclickListener = onNoOnclickListener;

}

/**

* 设置确定按钮的显示内容和监听

* @param str

* @param onYesOnclickListener

*/

public void setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener) {

if (str != null) {

yesStr = str;

}

if (yesStr != null && positiveButton != null) {//防止该方法在onCreate后执行(log是在前面执行的,有点不可思议)

positiveButton.setText(yesStr);

}

this.yesOnclickListener = onYesOnclickListener;

}

5,初始化焦点,部分电视上,需要代码拿焦点,不会自己取焦点

public void initFocus(boolean focusPos) {

if (positiveButton == null) return;

if (negativeButton == null) return;

if (focusPos) {

positiveButton.requestFocus();

} else {

negativeButton.requestFocus();

}

}

6,外部调用

/**

* 退出对话框

*/

private DialogChoice02 dialogChoice02;

private void showExitDialog() {

if (dialogChoice02 == null)

dialogChoice02 = new DialogChoice02(mContext);

dialogChoice02.setTitle("即将退出每家相册");

dialogChoice02.setYesOnclickListener("残忍离开", new DialogChoice02.onYesOnclickListener() {

@Override

public void onYesClick() {

BrowserCallback.getInstance().exit();

ImAidlManage.getInstance().exit();

MyApplication.getInstance().exit();

}

});

dialogChoice02.setNoOnclickListener("留下看看", new DialogChoice02.onNoOnclickListener() {

@Override

public void onNoClick() {

}

});

dialogChoice02.initFocus(true);

dialogChoice02.show();

}

//设置dialog显示的位置

Window window =dialogChoice02.getWindow();

window.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL);

//上面两句可以设置显示的大概位置上下左右, 居中。

//下面的可以设置dialog具体在屏幕的坐标

WindowManager.LayoutParams params = window.getAttributes();

params.x = 10; 设置x坐标【取绝对值,负值无效】

params.y = 100; //设置y坐标

params.width = 220; //dialog宽

params.height = 200; //dialog高

window.setAttributes(params);

放图:

26037f3c0bb0b0640884c42fccaf6fc3.png

圆角shape 选中效果用selector实现。

安卓TV自定义dialog实现相关教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值