Android - EditText在点击布局后丢失内容

时间:2018-01-30 15:52:09

标签: android android-layout android-edittext

您好FrameLayout,其中包含RelativeLayoutEditText位于RelativeLayout。事情就是当我点击EditText并在其中键入一些文字然后点击布局时,键盘消失(假设我没有点击另一个EditText而是RelativeLayout)内容EditText消失了。

第一个问题是它消失的原因,以及第二个如何存储EditText中写入的数据,即使我点击EditText之外?

点击布局之前

enter image description here

点击布局后

enter image description here

1 个答案:

答案 0 :(得分:0)

@firegloves当然,这是托管片段的活动

    public class AddTicketActivity extends AppCompatActivity {

private Toolbar toolbar;
private DatePicker datePicker;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_ticket);
    datePicker = (DatePicker) findViewById(R.id.datePicker);
    toolbar = (Toolbar) findViewById(R.id.toolbar);


    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);


    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(AddTicketActivity.this, MainActivity.class);
            startActivity(intent);
        }
    });

    Calendar calendar= Calendar.getInstance();
       MyOnDateChangeListener onDateChangeListener = new MyOnDateChangeListener();
            datePicker.init(calendar.get(Calendar.YEAR),
            calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH), onDateChangeListener);


}

public class MyOnDateChangeListener implements DatePicker.OnDateChangedListener {



    @Override
    public void onDateChanged(DatePicker view, int year, int month, int day) {
        int monthNumber= month+1;
        int yearNumber= year;
        int dayNumber = day;
        String date = String.valueOf(yearNumber)+"/"+String.valueOf(monthNumber)+"/"+String.valueOf(dayNumber);

        Fragment fragment = null;
        fragment = new AddTicketFragment();
        Bundle bundle = new Bundle();
        bundle.putString("date",date);
        fragment.setArguments(bundle);
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.relativeReplace, fragment).addToBackStack("AddTicketActivity").commit();

    }


}


}

这是片段

    public class AddTicketFragment extends Fragment {





private NumberPicker numberPicker;
private EditText deposit;
private EditText rate;
private EditText possibleWin;
private FloatingActionButton submit;
private Context context;
private Toolbar toolbar;

public AddTicketFragment() {
    // Required empty public constructor
}




@SuppressLint("NewApi")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{

    View view = inflater.inflate(R.layout.fragment_add_ticket,container,false);
    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    toolbar = (Toolbar) view.findViewById(R.id.toolbar_fragment_Add);
    numberPicker =(NumberPicker) view.findViewById(R.id.numberOfMatches);
    deposit =(EditText) view.findViewById(R.id.deposit);
    rate= (EditText) view.findViewById(R.id.rate);
    possibleWin = (EditText) view.findViewById(R.id.win);
    submit =(FloatingActionButton) view.findViewById(R.id.FAB2);

    context=getActivity().getApplicationContext();
    final DatabaseOperations db = new DatabaseOperations(context);
    db.getReadableDatabase();


    deposit.setGravity(Gravity.CENTER);
    rate.setGravity(Gravity.CENTER);
    possibleWin.setGravity(Gravity.CENTER);
    numberPicker.setMaxValue(30);
    numberPicker.setMinValue(1);
    numberPicker.setWrapSelectorWheel(true);











    ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);


            Bundle bundle = this.getArguments();
    final String dateNumber = bundle.getString("date",null);

    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(!deposit.getText().toString().isEmpty()){
                if(!rate.getText().toString().isEmpty()){

                        String numberOfMatches= String.valueOf(numberPicker.getValue());
                        String depositNumber= String.valueOf(deposit.getText());
                        String rateNumber=String.valueOf(rate.getText());
                        float multiply = Float.valueOf(depositNumber)*Float.valueOf(rateNumber);

                        possibleWin.setText(String.valueOf(multiply));
                        String winNumber=String.valueOf(possibleWin.getText());

                        db.insertMatches(db,dateNumber,numberOfMatches,rateNumber,depositNumber,winNumber);
                        Toast.makeText(getActivity().getApplicationContext(), "Ticket has been added", Toast.LENGTH_LONG).show();

                        Intent i = new Intent(getActivity(), MainActivity.class);
                        startActivity(i);

                }
                else{
                    Toast.makeText(getActivity().getApplicationContext(), "You need to fill rate !", Toast.LENGTH_SHORT).show();
                }
            }
            else{
                Toast.makeText(getActivity().getApplicationContext(), "You need to fill deposit ! ", Toast.LENGTH_SHORT).show();
            }
        }
    });
    return view ;
    }



    }