package com.example.bottomnavigationaddsubmult.ui.sub; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.bottomnavigationaddsubmult.MainActivity; import com.example.bottomnavigationaddsubmult.R; import android.content.Context; import android.content.Intent; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.*; import com.example.bottomnavigationaddsubmult.Basis; //import com.example.vorldbottomnavigationaddsubmult.MainActivity; //import com.example.vorldbottomnavigationaddsubmult.R; import java.util.Random; import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputMethodManager; public class SubFragment extends Fragment { private static final String TAG = SubFragment.class.getSimpleName(); private TextView tvtitle=null; private EditText tnumber1 = null; private EditText tnumber2 = null; private EditText tnumber3 = null; private TextView tvcomment = null; private Button bntest = null; private Button bnagain = null; private int zahl1=0; private int zahl2=0; private int ergebnis=0; public SubFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_sub, container, false); tvtitle = view.findViewById(R.id.tvtitlesub); tnumber1 = view.findViewById(R.id.tnumber1); tnumber2 = view.findViewById(R.id.tnumber2); tnumber3 = view.findViewById(R.id.tnumber3); tvcomment = view.findViewById(R.id.tvcomment); bntest = (Button) view.findViewById(R.id.bntest); bnagain = (Button) view.findViewById(R.id.bnagain); bntest.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { bntest_Click(); } } ); bnagain.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { bnagain_Click(); } } ); //InputMethodManager in = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); tnumber3.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_GO){ // auf goEvent return true; } if (actionId == EditorInfo.IME_ACTION_NEXT){ // auf goEvent return true; } if (actionId == EditorInfo.IME_ACTION_SEND){ // auf goEvent return true; } if (actionId == EditorInfo.IME_ACTION_DONE){ // grĂ¼ne Haken // //MainActivity.in.hideSoftInputFromWindow(tnumber3.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS); bntest_Click(); return false; } return false; } }); setRandomNumbers(); return view; } // oncreate private void bntest_Click() { Integer Zahl3 = Basis.getIntegerNumber(tnumber3.getText().toString()); if (Zahl3==null) { tvcomment.setText(getText(R.string.wrongnumberformat)); } else { int eingabeErgebnis = Zahl3.intValue(); if (eingabeErgebnis==this.ergebnis) { tvcomment.setText(getText(R.string.correctnumber)); } else { tvcomment.setText(getText(R.string.wrongnumber)); } } } private void bnagain_Click() { setRandomNumbers(); } private void setRandomNumbers() { Random rand = new Random(); this.zahl1 = rand.nextInt(200)+1; this.zahl2 = rand.nextInt(200)+1; this.ergebnis = this.zahl1-this.zahl2; tnumber1.setText(Integer.toString(this.zahl1)); tnumber2.setText(Integer.toString(this.zahl2)); tnumber3.setText("?"); tvcomment.setText(""); } }