【问题标题】:TouchEvent in a Dialog AndroidAndroid 对话框中的 TouchEvent
【发布时间】:2012-04-25 21:35:15
【问题描述】:

您好,我正在开发一个应用程序,在该应用程序中我正在使用活动对话框。对话框将只显示一张图片,当用户触摸图片时,对话框应该关闭,并且启动对话框的活动应该出现在前面。 对话框没有任何按钮

我在这个应用程序中的问题是我无法在对话框中获取触摸事件,我尝试在互联网上搜索解决此问题的方法,但找不到合适的方法实施它。那么任何人都可以建议一种这样做的方法......

【问题讨论】:

  • 谢谢,我已经为我所有的问题选择了正确的答案。
  • nice.... 因为这甚至让用户回答您的问题更具吸引力......对于这个问题?还有问题吗?

标签: android dialog


【解决方案1】:

像这样试试..

  final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.mylayout);
   //craete a layout with imageview
        dialog.setTitle("Title...");

        ImageView image = (ImageView) dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.yourimage);

        image.setOnClickListener(new View.OnClickListener(){
         public void onClick(View View3) {
             //your onclick functionality
         } });

        });

        dialog.show();

【讨论】:

  • 它工作得很好,你能建议我如何在没有标题栏的情况下全屏显示对话框。
  • 查看此链接.. 我认为它会有所帮助.. stackoverflow.com/questions/1362723/…
  • 这个问题的解决方案是针对一个活动,但在我的应用程序中,我需要一个全屏的对话框。
【解决方案2】:

我相信在 Dialog 的主要布局元素上设置 View.OnClickListener 可以解决问题 :-)

示例代码:

    public class DialogActivity extends Activity implements OnClickListener
    {
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.dialog);
            LinearLayout linearlayout = (LinearLayout)findViewById(R.id.dialogMainLayout);
            linearlayout.setOnClickListener(this);
        }

        public void onClick(View v)
        {
            finish();
        }
    }

dialog.xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:background="#FFA500"
        android:id="@+id/dialogMainLayout"
        >
        <TextView 
            android:layout_height="250dp"
            android:layout_width="fill_parent"
            android:text="TEST"
            android:background="#FFFFF0"
            />
    </LinearLayout>

首先,我尝试制作一个将启动另一个主题作为对话的活动。单击以对话框为主题的活动完全没有做任何事情。所以我认为在对话框主题活动中使用的 xml 文件中的根布局元素上设置 onclicklistener 可能会解决问题。

我得到了上面的代码。希望它能解决你的问题:-)

【讨论】:

  • 感谢您的回复和代码,但我希望它们在对话框中,而不是像对话框这样的活动。
【解决方案3】:

Android: Close dialog window on touch

这个问题可能会为您的问题提供答案。或者,您可以使用 LayoutInflater 创建一个自定义视图,其中包含一个 ImageButton 或 ImageView 以及一个 OnClickListener 和 AlertDialog.Builder 及其 setView 方法,以使该视图成为您的对话框的主体。

【讨论】:

    猜你喜欢
    • 2014-08-05
    • 2016-09-03
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 2016-08-07
    • 2011-04-28
    • 1970-01-01
    相关资源
    最近更新 更多