成都创新互联网站制作重庆分公司

如何在Android中自定义Dialog

本篇文章为大家展示了如何在Android中自定义Dialog,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

广灵网站建设公司成都创新互联公司,广灵网站设计制作,有大型网站制作公司丰富经验。已为广灵上千家提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的广灵做网站的公司定做!

功能:

android 提供给我们的只有2种Dialog 即 AlertDialog & ProgressDialog 但是 Dialog 有其自身的特点:1. 不是 Activity 2. 开销比 Activity 小得多

原理:

1. android 系统提供了一个class: Dialog. 而且你可以把自己的工作放在"protected void onCreate(Bundle savedInstanceState)" 在里面先调用系统的"super.onCreate(savedInstanceState)" 其就会保证调用这个method.

2. 至于 Dialog 界面的定制 可以写一个xml 文件 然后 在 "void onCreate(Bundle)" 通过 "setContentView()" 来使之生效

3. Dialog 使用问题: 1. 弹出:show() 2. 取消:dismiss()

代码:

1. 创建一个 Dialog:

public class CustomDialog extends Dialog {
  public CustomDialog(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
  }
  protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custom_dialog);
    setTitle("Custom Dialog");
    TextView text = (TextView)findViewById(R.id.text);
    text.setText("Hello, this is a custom dialog!");
    ImageView image = (ImageView)findViewById(R.id.image);
    image.setImageResource(R.drawable.sepurple);
    Button buttonYes = (Button) findViewById(R.id.button_yes);
    buttonYes.setHeight(5);
    buttonYes.setOnClickListener(new Button.OnClickListener(){
      public void onClick(View v) {
        // TODO Auto-generated method stub
        dismiss();
      }
    });
    Button buttonNo = (Button) findViewById(R.id.button_no);
    buttonNo.setSingleLine(true);
    buttonNo.setOnClickListener(new Button.OnClickListener(){
      public void onClick(View v) {
        // TODO Auto-generated method stub
        dismiss();
      }
    });
  }
  //called when this dialog is dismissed
  protected void onStop() {
    Log.d("TAG","+++++++++++++++++++++++++++");
  }
}

2. Dialog 的使用:

public class CustomDialogUsage extends Activity {
  CustomDialog cd;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    cd = new CustomDialog(this);
    Button buttonYes = (Button) findViewById(R.id.main_button);
    buttonYes.setOnClickListener(new OnClickListener(){
      public void onClick(View v) {
        // TODO Auto-generated method stub
        cd.show();
      }
    });
  }
}

3. Dialog 的界面定制:


  
  
    
    
      

上述内容就是如何在Android中自定义Dialog,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


本文名称:如何在Android中自定义Dialog
URL网址:http://cxhlcq.com/article/ggoooc.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部