简体   繁体   English

Android在布局内关闭自定义对话框

[英]Android close custom dialog within layout

I am looking for a way to close a custom dialog with a button that is inside the xml used in the dialog, alternatively closing it by pressing anywhere on the dialog. 我正在寻找一种使用对话框中使用的xml内的按钮关闭自定义对话框的方法,或者通过按对话框中的任意位置来关闭它。 What I have is this; 我有这个 a layout with a Image Button that brings up the custom dialog with the content. 具有图像按钮的布局,该按钮会显示带有内容的自定义对话框。 I have setCanceledOnTouchOutside(true); 我有setCanceledOnTouchOutside(true); and that works, but I need the dialog to fill up most of the screen and it can be hard for the user to click in the small space that is available. 可以,但是我需要对话框来填满屏幕的大部分内容,并且用户很难在可用的小空间中单击。 So how do I do this? 那么我该怎么做呢?

My java code: 我的Java代码:

import android.app.Activity;
import android.app.Dialog;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class Rose extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setRequestedOrientation(
        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.rose);

        ImageButton b = (ImageButton) findViewById(R.id.imageButton1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Dialog d1 = new Dialog(Rose.this);
                d1.setContentView(R.layout.tariquet);
                d1.setCanceledOnTouchOutside(true);
                d1.show();  


            }
        });
    }

}

And my XML: 而我的XML:

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

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:fitsSystemWindows="true" 
    android:isScrollContainer="true"
    android:minHeight="1100dp" 
    android:minWidth="650dp">
    <ImageView 
    android:src="@drawable/rose_tariquet" 
    android:id="@+id/imageView1" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"></ImageView>
    <Button android:text="X" 
    android:layout_height="wrap_content" 
    android:id="@+id/button1" 
    android:layout_width="55dp" 
    android:layout_gravity="right"></Button>

</FrameLayout>
public class CustomizeDialog extends Dialog implements OnClickListener {
Button close;
TextView tv;
public CustomizeDialog(Context context,String Stringcontent) {
    super(context);
    requestWindowFeature(Window.FEATURE_NO_TITLE);      
    setContentView(R.layout.custom_diolog_main);
    tv=(TextView) findViewById(R.id.content);
    tv.setText(Stringcontent);
    close = (Button) findViewById(R.id.close);
    close.setOnClickListener(this);
}

@Override
public void onClick(View v) {       
    if (v == close)
        dismiss();
}
}

called: 称为:

CustomizeDialog customizeDialog = new CustomizeDialog(CustomDialog.this,"clickme");
customizeDialog.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM