对话+回调? [英] Dialog + callback?

查看:67
本文介绍了对话+回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了CustomTypeDialog类,我想要使用不在活动布局中的EditText.当我尝试单击按钮之一时,我得到了一个nullpointer异常,我认为这是因为它们不在活动布局中.你能帮我解决这个问题吗?该对话框是在另一个类的活动中调用的.

I have made a CustomTypeDialog class and what I want is to use EditText which is not in the active layout. I get a nullpointer exception when I try to click one of the buttons, which I think is because they are not in the active layout. Can you help me solve this? The dialog is called in an activity from another class.

package dk.droidrun.droidrunapp;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class CustomTypeDialog extends Dialog {
    ImageButton routeType;
    EditText txtType;
    Button imageRun, imageBike, imageWalk;
    public CustomTypeDialog(final Context context) {
        super(context);

        this.setContentView(R.layout.customtype_dialog);
        routeType = (ImageButton)findViewById(R.id.saveRoute_activityType);
        txtType = (EditText)findViewById(R.id.saveRoute_typeTxt);
        imageRun = (Button)findViewById(R.id.dialog_btn1);
        imageBike = (Button)findViewById(R.id.dialog_btn2);
        imageWalk = (Button)findViewById(R.id.dialog_btn3);

        setTitle("Select activity type");
        show();
        imageRun.setOnClickListener(new View.OnClickListener() {    
            @Override
            public void onClick(View v) {
                txtType.setText("Run");
                routeType.setBackgroundResource(R.drawable.track_run);
            }
        });

        imageBike.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                txtType.setText("Bike");
                routeType.setBackgroundResource(R.drawable.track_bike);
            }
        });

        imageWalk.setOnClickListener(new View.OnClickListener() {       
            @Override
            public void onClick(View v) {
                txtType.setText("Walk");
                routeType.setBackgroundResource(R.drawable.track_walk);
            }
        });
    }

} 

这是我的customtype_dialog.xml

This is my customtype_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AutoMode"
    android:background="@color/black" >    

<RelativeLayout 
    android:id="@+id/dialog_relativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="3dp"    
    android:layout_above="@+id/dialog_relativeLayout2"
    android:layout_centerHorizontal="true" >

    <Button
        android:id="@+id/dialog_btn1"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="3dp"
        android:background="@drawable/track_run"
        android:layout_alignRight="@+id/dialog_relativeLayout1"
        android:layout_alignTop="@+id/dialog_relativeLayout1"
        />

    <Button 
        android:id="@+id/dialog_btn2"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="3dp"
        android:background="@drawable/track_bike"
        android:layout_alignTop="@+id/dialog_relativeLayout1"
        android:layout_toRightOf="@+id/dialog_btn1"
        />

    <Button 
        android:id="@+id/dialog_btn3"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="3dp"
        android:layout_toRightOf="@+id/dialog_btn2"
        android:background="@drawable/track_walk"
        />
 </RelativeLayout>


</RelativeLayout>

saveroutes.xml

saveroutes.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/black"
    tools:context=".SaveRouteActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="@string/saveRoute"
        android:textColor="@color/white"
        android:textAppearance="?android:attr/textAppearanceLarge" />

   <TextView
        android:layout_marginTop="10dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#7a0100"
        android:text="Enter a name for the route" /> 

    <EditText
        android:id="@+id/saveRoute_nameRoute"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"        
        android:ems="10"
        android:hint="@string/saveRoute_name" 
        android:textColor="@color/white" 
        android:background="#4e4751"      
        android:inputType="textPersonName" >
    </EditText>

    <TextView        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#7a0100"
        android:text="Describe your route" />

    <EditText
        android:id="@+id/saveRoute_desciptionTxt"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ems="10"
        android:hint="@string/saveRoute_description"
        android:textColor="@color/white"
        android:background="#4e4751"    
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#7a0100"
        android:text="Activity type (e.g. running)"
         />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"       
        android:orientation="horizontal"
        android:gravity="center_horizontal" >

        <EditText
            android:id="@+id/saveRoute_typeTxt"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_marginTop="17dip"
            android:ems="10"
            android:layout_marginLeft="30dp"
            android:hint="@string/saveRoute_type"
            android:textColor="@color/white"
            android:background="#4e4751"     >

            <requestFocus />
        </EditText>

        <ImageButton
            android:id="@+id/saveRoute_activityType"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"          
            android:src="@drawable/track_walk" />
    </LinearLayout>

    <Button
        android:id="@+id/saveRoute_saveBtn"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:background="@color/white"
        android:text="@string/saveRoute_savebutton" />

    <Button
        android:id="@+id/saveRoute_cancelBtn"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:background="@color/white"
        android:text="@string/saveRoute_cancel" />

</LinearLayout>

推荐答案

您无法使用对话框视图中的findViewById访问其他布局(您的活动)中的视图.

You cannot access views from another layout (your activity) using findViewById within the dialog view.

单击对话框上的按钮时,需要添加一个回调侦听器:

You need to add a callback listener for when the buttons on your dialog are clicked:

public interface OnDialogClickListener {
    void onDialogImageRunClick();
}

public class CustomTypeDialog extends Dialog {
    private final OnDialogClickListener listener;
    public CustomTypeDialog(final Context context, OnDialogClickListener listener) {
          this.listener = listener;
    }

     ....
     imageRun.setOnClickListener(new View.OnClickListener() {    
            @Override
            public void onClick(View v) {
                  listener.onDialogImageRunClick();
            }
    );
}

然后,当您在可以访问视图的活动中创建对话框时,

Then when you create your dialog in your Activity where you have access to the view:

     new CustomTypeDialog(context, new CustomTypeDialog.OnDialogClickListener() {
            @Override
            public void onDialogImageRunClick() {
                   txtType.setText("Run");
                   routeType.setBackgroundResource(R.drawable.track_run);
            }
      });

这篇关于对话+回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆