Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package validate.com.validate.merchant;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.text.Editable;
- import android.text.TextWatcher;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.inputmethod.InputMethodManager;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.google.zxing.integration.android.IntentIntegrator;
- import com.google.zxing.integration.android.IntentResult;
- import validate.com.validate.R;
- /**
- * Created by Mina on 06/06/2015.
- */
- public class validate_fragment extends Fragment implements View.OnClickListener, View.OnFocusChangeListener {
- private static final String TAG = "validate_fragment";
- StringBuilder stringBuilder1;
- StringBuilder stringBuilder2;
- StringBuilder stringBuilder3;
- boolean focus1 = false, focus2 = false, focus3 = false;
- private static int QR_REQUEST_CODE = 100;
- private Button scanqr, check, button0, button1, button2, button3,
- button4, button5, button6, button7, button8, button9, clearbtn, canclebtn, delbtn;
- public EditText mTextView, mTextView2, mTextView3;
- private static final int MAX_LENGTH = 3;
- InputMethodManager imm;
- public validate_fragment() {
- }
- @Override
- public void onCreate(@Nullable Bundle savedInstanceState) {
- setRetainInstance(true);
- stringBuilder1 = new StringBuilder();
- stringBuilder1 = new StringBuilder();
- stringBuilder1 = new StringBuilder();
- imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
- super.onCreate(savedInstanceState);
- }
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.validate_frag, null, false);
- setupUI(view);
- return view;
- }
- public void ScanQR() {
- IntentIntegrator integrator = new IntentIntegrator(getActivity());
- integrator.setPrompt("place QR code in window to validate");
- integrator.initiateScan();
- }
- private void setupUI(View view) {
- //declare textviews
- mTextView = (EditText) view.findViewById(R.id.code1);
- mTextView.setOnFocusChangeListener(this);
- mTextView.requestFocus();
- mTextView2 = (EditText) view.findViewById(R.id.code2);
- mTextView2.setOnFocusChangeListener(this);
- mTextView3 = (EditText) view.findViewById(R.id.code3);
- mTextView3.setOnFocusChangeListener(this);
- mTextView.addTextChangedListener(new TextWatcher() {
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {
- }
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- }
- @Override
- public void afterTextChanged(Editable s) {
- if (mTextView.getText().length() == MAX_LENGTH) {
- mTextView.clearFocus();
- mTextView2.requestFocus();
- }
- }
- });
- mTextView2.addTextChangedListener(new TextWatcher() {
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {
- }
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- }
- @Override
- public void afterTextChanged(Editable s) {
- if (mTextView2.getText().length() == MAX_LENGTH) {
- mTextView2.clearFocus();
- mTextView3.requestFocus();
- }
- }
- });
- //declare pad button
- button0 = (Button) view.findViewById(R.id.button0);
- button0.setOnClickListener(this);
- button1 = (Button) view.findViewById(R.id.button1);
- button1.setOnClickListener(this);
- button2 = (Button) view.findViewById(R.id.button2);
- button2.setOnClickListener(this);
- button3 = (Button) view.findViewById(R.id.button3);
- button3.setOnClickListener(this);
- button4 = (Button) view.findViewById(R.id.button4);
- button4.setOnClickListener(this);
- button5 = (Button) view.findViewById(R.id.button5);
- button5.setOnClickListener(this);
- button6 = (Button) view.findViewById(R.id.button6);
- button6.setOnClickListener(this);
- button7 = (Button) view.findViewById(R.id.button7);
- button7.setOnClickListener(this);
- button8 = (Button) view.findViewById(R.id.button8);
- button8.setOnClickListener(this);
- button9 = (Button) view.findViewById(R.id.button9);
- button9.setOnClickListener(this);
- // declare function buttons
- clearbtn = (Button) view.findViewById(R.id.buttonclear);
- clearbtn.setOnClickListener(this);
- canclebtn = (Button) view.findViewById(R.id.buttoncancel);
- canclebtn.setOnClickListener(this);
- delbtn = (Button) view.findViewById(R.id.buttonDel);
- delbtn.setOnClickListener(this);
- check = (Button) view.findViewById(R.id.buttoncheck);
- check.setOnClickListener(this);
- scanqr = (Button) view.findViewById(R.id.scanqr);
- scanqr.setOnClickListener(this);
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- IntentResult ScanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
- if (ScanResult != null) {
- String content = ScanResult.getContents();
- String format = ScanResult.getFormatName();
- Toast.makeText(getActivity(), "contents:" + content + "formate:" + format, Toast.LENGTH_LONG).show();
- } else {
- Toast.makeText(getActivity(),
- "No scan data received!", Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.button0:
- writeNumber(0);
- break;
- case R.id.button1:
- writeNumber(1);
- break;
- case R.id.button2:
- writeNumber(2);
- break;
- case R.id.button3:
- writeNumber(3);
- break;
- case R.id.button4:
- writeNumber(4);
- break;
- case R.id.button5:
- writeNumber(5);
- break;
- case R.id.button6:
- writeNumber(6);
- break;
- case R.id.button7:
- writeNumber(7);
- break;
- case R.id.button8:
- writeNumber(8);
- break;
- case R.id.button9:
- writeNumber(9);
- break;
- case R.id.buttoncancel:
- break;
- case R.id.buttonclear:
- clear();
- break;
- case R.id.buttonDel:
- deletelastnumber();
- break;
- case R.id.scanqr:
- ScanQR();
- break;
- }
- }
- private void writeNumber(int n) {
- if (focus1 && stringBuilder1.length() < 3) {
- stringBuilder1.append(n);
- mTextView.setText(stringBuilder1.toString());
- mTextView.setSelection(stringBuilder1.length());
- } else if (focus2 && stringBuilder2.length() < 3) {
- stringBuilder2.append(n);
- mTextView2.setText(stringBuilder2.toString());
- mTextView2.setSelection(stringBuilder2.length());
- } else if (focus3 && stringBuilder3.length() < 3) {
- stringBuilder3.append(n);
- mTextView3.setText(stringBuilder3.toString());
- mTextView3.setSelection(stringBuilder3.length());
- }
- }
- private void clear() {
- if (mTextView != null) {
- mTextView.getText().clear();
- stringBuilder1 = new StringBuilder();
- }
- if (mTextView2 != null) {
- mTextView2.getText().clear();
- stringBuilder2 = new StringBuilder();
- }
- if (mTextView3 != null) {
- stringBuilder3 = new StringBuilder();
- mTextView3.getText().clear();
- }
- }
- private void deletelastnumber() {
- if (focus1 && stringBuilder1.length() > 0) {
- if (stringBuilder1.length() == 1) {
- stringBuilder1.deleteCharAt(stringBuilder1.length() - 1);
- mTextView.getText().clear();
- } else {
- stringBuilder1.deleteCharAt(stringBuilder1.length() - 1);
- mTextView.setText(stringBuilder1.toString());
- mTextView.setSelection(stringBuilder1.length());
- }
- } else if (focus2 && stringBuilder2.length() > 0) {
- if (stringBuilder2.length() == 1) {
- stringBuilder2.deleteCharAt(stringBuilder2.length() - 1);
- mTextView2.getText().clear();
- } else {
- stringBuilder2.deleteCharAt(stringBuilder2.length() - 1);
- mTextView2.setText(stringBuilder2.toString());
- mTextView2.setSelection(stringBuilder1.length());
- }
- } else if (focus3 && stringBuilder3.length() > 0) {
- if (stringBuilder3.length() == 1) {
- stringBuilder3.deleteCharAt(stringBuilder3.length() - 1);
- mTextView3.getText().clear();
- } else {
- stringBuilder3.deleteCharAt(stringBuilder3.length() - 1);
- mTextView3.setText(stringBuilder3.toString());
- mTextView3.setSelection(stringBuilder1.length());
- }
- }
- }
- @Override
- public void onFocusChange(View v, boolean hasFocus) {
- switch (v.getId()) {
- case R.id.code1:
- if (hasFocus) {
- imm.showSoftInputFromInputMethod(mTextView.getWindowToken(), 0);
- }
- break;
- case R.id.code2:
- if (hasFocus) {
- imm.showSoftInputFromInputMethod(mTextView2.getWindowToken(), 0);
- }
- break;
- case R.id.code3:
- if (hasFocus) {
- imm.showSoftInputFromInputMethod(mTextView3.getWindowToken(), 0);
- }
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement