我试图创建自定义对话框,但在调用 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() 不起作用...如果有人有其他建议我愿意听...提前致谢!