0

我试图创建自定义对话框,但在调用 show() 函数时出现错误。我不确定我是否错过了创建对话框的某些步骤。这是代码:

public class TrackerSettingsActivity extends Activity { 
Context context;
PhoneStateBroadcastReciever prefs;
Handler handler;
SharedPreferences m_sharedPrefs;
Editor editor;

EditText timerDelay;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.tracker_settings);

    timerDelay = (EditText) findViewById(R.id.editText_Set_Timer);
    m_sharedPrefs = getSharedPreferences("MyPrefs", 0);
    editor = m_sharedPrefs.edit();

    Button button = (Button) findViewById(R.id.button_Confirm);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            customDialog().show();

        }
         }
private Dialog customDialog() {
    final Dialog dialog = new Dialog(getBaseContext());
    dialog.setContentView(R.layout.login_dialog);
    dialog.setTitle("Title...");

    // set the custom dialog components - text, image and button
    TextView text = (TextView) dialog.findViewById(R.id.text);
    text.setText("Android custom dialog example!");
    ImageView image = (ImageView) dialog.findViewById(R.id.image);
    image.setImageResource(R.drawable.ic_launcher);

    Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
    // if button is clicked, close the custom dialog
    dialogButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    return dialog;
}


}

login_dialog.xml:

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="5dp"
    android:src="@drawable/ic_launcher" />

 <TextView
     android:id="@+id/text"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_alignParentLeft="true"
     android:layout_below="@+id/image"
     android:text="sss"
     android:textColor="#FFF" />

 <Button
     android:id="@+id/dialogButtonOK"
     android:layout_width="100px"
     android:layout_height="wrap_content"
     android:layout_alignParentLeft="true"
     android:layout_below="@+id/text"
     android:text=" Ok " />

tracker_settings.xml:

<TextView
    android:id="@+id/textView_Set_Timer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="68dp"
    android:text="@string/tv_settimer"
    android:textColor="#ffffff" />

<EditText
    android:id="@+id/editText_Set_Timer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/textView_Set_Timer"
    android:layout_below="@+id/textView_Set_Timer"
    android:ems="10"
    android:inputType="number"
    android:textColorHint="#ffffff" />

<Button
    android:id="@+id/button_Confirm"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText_Set_Timer"
    android:onClick="click"
    android:text="@string/bn_confirm" />

我不知道为什么 show() 不起作用...如果有人有其他建议我愿意听...提前致谢!

4

2 回答 2

2

使用这个:当你必须创建一个对话框时使用活动引用。

Actvity activity = this;
private Dialog customDialog() {
    final Dialog dialog = new Dialog(activity); // or new Dialog(YourActivity.this);
    dialog.setContentView(R.layout.login_dialog);
    ....

}
于 2013-10-31T10:34:26.127 回答
1

由于没有父视图,因此无法添加您的自定义对话框。正如@jzafrilla 所说,为您的构造函数添加一个活动引用,以便android 知道将其附加到什么。

... = new Dialog(Main.this); // or something similar

如果你这样做,那么你应该能够在父视图的顶部看到你的对话框

于 2013-10-31T10:40:03.627 回答