目标是要有一个菜单(位于menu_main xml上),一旦您单击“详细信息”,便会打开一个对话框(dialogDetails),该对话框代表一个详细信息表单,其中包括编辑文本,日期选择器和性别选择对话框。 请说,如果需要,我将上传xml文件(这是android studio中的java文件...) 非常感谢您的帮助:)
package com.example.android.work8;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.ClipData;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
Dialog dialogGender, dialogDetails;
Button btnSaveGender;
Button btnSave, btnBirthDate, btnGender;
EditText etFirstName, etLastName, etCity;
RadioGroup rg1;
SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etFirstName = (EditText) findViewById(R.id.etFirstName);
etLastName = (EditText) findViewById(R.id.etLastName);
etCity = (EditText) findViewById(R.id.etCity);
lastSave();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
int id = item.getItemId();
if (id == R.id.myDetails) {
createDetailsDialog();
} else if (id == R.id.delete) {
}
return true;
}
public void createGenderDialog() {
dialogGender = new Dialog(this);
dialogGender.setContentView(R.layout.custom_gender);
dialogGender.setTitle("Select your gender");
dialogGender.setCancelable(false);
btnSaveGender = (Button) dialogGender.findViewById(R.id.btnSaveGender);
btnSaveGender.setOnClickListener(this);
rg1 = (RadioGroup) dialogGender.findViewById(R.id.rg1);
rg1.setOnCheckedChangeListener(this);
dialogGender.show();
}
public void createDetailsDialog(){
dialogDetails = new Dialog(this);
dialogDetails.setContentView(R.layout.my_details);
dialogDetails.setTitle("Insert details");
dialogDetails.setCancelable(false);
btnSave=(Button)dialogDetails.findViewById(R.id.btnSave);
btnSave.setOnClickListener(this);
btnBirthDate=(Button)dialogDetails.findViewById(R.id.btnBirthDate);
btnBirthDate.setOnClickListener(this);
btnGender=(Button)dialogDetails.findViewById(R.id.btnGender);
btnGender.setOnClickListener(this);
dialogDetails.show();
}
@Override
public void onClick (View v){
if (btnGender == v) {
createGenderDialog();
} else if (btnBirthDate == v) {
Calendar systemCalender = Calendar.getInstance();
int year = systemCalender.get(Calendar.YEAR);
int month = systemCalender.get(Calendar.MONTH);
int day = systemCalender.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new SetDate(), year, month, day);
datePickerDialog.show();
} else if (btnSave == v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Save details");
builder.setMessage("do you want to save?");
builder.setCancelable(true);
builder.setPositiveButton("yes", new HandleAlertDialogListener());
builder.setNegativeButton("No", new HandleAlertDialogListener());
AlertDialog dialog = builder.create();
dialog.show();
} else if (btnSaveGender == v) {
dialogGender.dismiss();
}
}
@Override
public void onCheckedChanged (RadioGroup group,int checkedId){
if (group == rg1) {
if (checkedId == R.id.rMale) {
btnGender.setText("male");
} else if (checkedId == R.id.rFemale) {
btnGender.setText("female");
}
}
}
public class HandleAlertDialogListener implements DialogInterface.OnClickListener {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == -1) saveDetails();
else
Toast.makeText(MainActivity.this, "Details were not saved", Toast.LENGTH_LONG).show();
dialogDetails.dismiss();
}
}
public class SetDate implements DatePickerDialog.OnDateSetListener {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
monthOfYear = monthOfYear + 1;
String str = dayOfMonth + "/" + monthOfYear + "/" + year;
btnBirthDate.setText(str);
}
}
public void saveDetails () {
Toast.makeText(MainActivity.this, "Details were saved", Toast.LENGTH_LONG).show();
SharedPreferences sp = getSharedPreferences("details1", 0);
SharedPreferences.Editor editor = sp.edit();
editor.putString("fname", etFirstName.getText().toString()).putString("lname", etLastName.getText().toString());
editor.putString("city", etCity.getText().toString());
editor.putString("gender", btnGender.getText().toString());
editor.putString("bitrth", btnBirthDate.getText().toString());
editor.commit();
}
public void lastSave () {
SharedPreferences sp = getSharedPreferences("details1", 0);
String strfname = sp.getString("fname", null);
String strlname = sp.getString("lname", null);
String strcity = sp.getString("city", null);
String strgender = sp.getString("gender", null);
String strbirth = sp.getString("bitrth", null);
if (strfname != null)
etFirstName.setText(strfname);
else if (strlname != null)
etLastName.setText(strlname);
else if (strcity != null)
etCity.setText(strcity);
else if (strgender != null)
btnGender.setText(strgender);
else if (strbirth != null)
btnBirthDate.setText(strbirth);
}
}
//error message
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.work8, PID: 13392
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.android.work8.MainActivity.saveDetails(MainActivity.java:162)
at com.example.android.work8.MainActivity$HandleAlertDialogListener.onClick(MainActivity.java:138)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5691)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)