gpt4 book ai didi

java - 对话+回调?

转载 作者:行者123 更新时间:2023-11-29 09:44:22 26 4
gpt4 key购买 nike

我制作了一个 CustomTypeDialog 类,我想要的是使用不在 Activity 布局中的 EditText。当我尝试单击其中一个按钮时出现空指针异常,我认为这是因为它们不在 Activity 布局中。你能帮我解决这个问题吗?在另一个类的 Activity 中调用该对话框。

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

<?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>

保存路径.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>

最佳答案

您不能在对话框 View 中使用 findViewById 从另一个布局(您的 Activity )访问 View 。


当您的对话框上的按钮被点击时,您需要添加一个回调监听器:

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();
}
);
}

然后当您在可以访问 View 的 Activity 中创建对话框时:

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

关于java - 对话+回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16508105/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com