I'm trying to save an edittext and a datepicker but they always save both with a die of the two. The date picker when you click on Editext2 it opens the calendar where you click and it selects the date for edittext2.
I just need to know how I can save both by clicking the activity button.
I basically want to save the oil mileage on edittext1, and the date it was made on edittext2. the button will save for me!
OIL CLASS
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
public class oleo extends Activity implements View.OnClickListener {
private Button buttonSalvarOleo;
private SharedPreferences savednotes;
private EditText editTextOleo;
private EditText editTextData;
Button buttonOleoRec;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_oleo);
buttonOleoRec = (Button)findViewById(R.id.buttonOleoRec);
buttonOleoRec.setOnClickListener(this);
buttonSalvarOleo = (Button) findViewById(R.id.buttonSalvarOleo);
editTextOleo = (EditText) findViewById(R.id.editTextOleo);
savednotes = getSharedPreferences("notes",MODE_PRIVATE);
editTextData = (EditText) findViewById(R.id.editTextData);
editTextOleo.setText(savednotes.getString("tag", "00.000")); //adicionar essa linha
buttonSalvarOleo.setOnClickListener(saveButtonListener);
}
//AQUI EH OQUE CHAMA O CALENDARIO
public void onStart(){
super.onStart();
EditText editTextData = (EditText)findViewById(R.id.editTextData);
editTextData.setOnFocusChangeListener(new View.OnFocusChangeListener(){
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
DateDialog dialog= new DateDialog(v);
FragmentTransaction ft = getFragmentManager().beginTransaction();
dialog.show(ft, "DatePicker");
}
}
});
}
private void makeTag(String tag){
String or = savednotes.getString(tag, null);
SharedPreferences.Editor preferencesEditor = savednotes.edit();
preferencesEditor.putString("tag",tag); //change this line to this
preferencesEditor.commit();
}
public View.OnClickListener saveButtonListener = new View.OnClickListener(){
@Override
public void onClick(View v) {
if(editTextOleo.getText().length()>0){
makeTag(editTextOleo.getText().toString());
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(editTextOleo.getWindowToken(),0);
}
}
};
private void buttonOleoRecClick(){
//NOME DO LAYOUT Q ESTA NO MANIFEST
startActivity(new Intent("layout.activity_oleorecomendado"));
}
@Override
public void onClick(View v) {
switch (v.getId()){
//fazer pros outros botoes aki
case R.id.buttonOleoRec:
buttonOleoRecClick();
break;
}
}
}
DATEDIALOG CLASS
import android.annotation.TargetApi;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import java.util.Calendar;
/**
* Created by Bruno Marna on 9/19/2015.
*/
public class DateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener{
EditText editTextData;
public DateDialog(View view){
editTextData=(EditText)view;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public Dialog onCreateDialog(Bundle savedInstanceState){
final Calendar c=Calendar.getInstance();
int year =c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH);
int day=c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this,year,month,day);
}
public void onDateSet(DatePicker view, int year, int month, int day){
String date=day+"/"+(month+1)+"/"+year;
editTextData.setText(date);
}
}
ACTIVITY
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:id="@+id/editTextOleo"
android:layout_alignBottom="@+id/textView"
android:layout_toRightOf="@+id/textView"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Salvar"
android:id="@+id/buttonSalvarOleo"
android:layout_weight="1.12"
android:layout_gravity="right|center_vertical"
android:background="#444"
android:textColor="#FFF"
android:textStyle="bold"
android:layout_marginTop="33dp"
android:layout_below="@+id/editTextData"
android:layout_alignRight="@+id/textView3"
android:layout_alignEnd="@+id/textView3" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editTextData"
android:hint="dia/mês/ano"
android:layout_alignBottom="@+id/textView10"
android:layout_alignLeft="@+id/editTextOleo"
android:layout_alignStart="@+id/editTextOleo" />