小编dan*_*nea的帖子

在java Android中为自定义Dialog创建一个通用类

我的应用程序显示许多自定义对话框,如是/否或接受/取消决定,当我编码时,我意识到有相当多的代码重复,遵循相同的架构.

我想构建一个通用类,但我不知道该怎么做,或者更确切地说,我必须要做的正确方法(接口,抽象类,继承,静态类......)

这是我目前的课程:

public class DialogTwoOptions extends Dialog {

TextView title_tv;
// Button yes_btn, no_btn;

public DialogTwoOptions(Context context) 
{
    super(context);     
    setContentView(R.layout.dialogo_sino); // a simple layout with a TextView and Two Buttons

    title_tv = (TextView) findViewById(R.id.dialogo_titulo_sino);
    // yes_btn = (Button) findViewById(R.id.dialogo_aceptar); 
    // no_btn = (Button) findViewById(R.id.dialogo_cancelar);

    View v = getWindow().getDecorView();
    v.setBackgroundResource(android.R.color.transparent);
}

 public void quitDialog(View v) {
     if (isShowing()) dismiss();
 }

 public void setTitle(String title) {
     title_tv.setText(title);
 }
Run Code Online (Sandbox Code Playgroud)

}

当我需要使用这个类时,这就是我正在做的事情:

final DialogTwoOptions dialog = new DialogTwoOptions(this);

    Button yes = (Button) dialog.findViewById(R.id.dialog_yes_btn); …
Run Code Online (Sandbox Code Playgroud)

java generics android dialog

21
推荐指数
2
解决办法
3万
查看次数

标签 统计

android ×1

dialog ×1

generics ×1

java ×1