Advertisement
anhit92

TestCode

Nov 30th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.57 KB | None | 0 0
  1. package com.example.tabhost.basic;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.app.Activity;
  5. import android.inputmethodservice.Keyboard;
  6. import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
  7. import android.os.Bundle;
  8. import android.text.Editable;
  9. import android.text.TextWatcher;
  10. import android.util.Log;
  11. import android.view.KeyEvent;
  12. import android.view.MotionEvent;
  13. import android.view.View;
  14. import android.view.View.OnKeyListener;
  15. import android.view.animation.Animation;
  16. import android.view.animation.AnimationUtils;
  17. import android.widget.AdapterView;
  18. import android.widget.EditText;
  19. import android.widget.Spinner;
  20. import android.widget.TextView;
  21. import android.widget.Toast;
  22.  
  23. import com.example.keyboard.BasicOnKeyboardActionListener;
  24. import com.example.keyboard.CustomKeyboardView;
  25. import com.example.uniticoverter.R;
  26.  
  27. @SuppressLint({ "ClickableViewAccessibility", "CutPasteId" })
  28. public class LengtH extends Activity implements OnKeyListener,
  29.         OnKeyboardActionListener {
  30.     private static final String TAG = LengtH.class.getName();
  31.     private CustomKeyboardView mKeyboardView;
  32.     private View mTargetView;
  33.     private Keyboard mKeyboard;
  34.     private EditText mtext;
  35.     private TextView edtmm;
  36.     private TextView edtcm;
  37.     private TextView edtdm;
  38.     private TextView edtm;
  39.     private TextView edkm;
  40.     private TextView edinch;
  41.     private TextView edft;
  42.     private TextView edyd;
  43.     private TextView edmile;
  44.     private TextView ednmile;
  45.     private String item;
  46.     private int kqKeyBoard;
  47.  
  48.     @Override
  49.     protected void onCreate(Bundle savedInstanceState) {
  50.         // TODO Auto-generated method stub
  51.         super.onCreate(savedInstanceState);
  52.         setContentView(R.layout.basic_length);
  53.         // kqKeyBoard = 0;
  54.         edtmm = (TextView) findViewById(R.id.tvmm);
  55.         edtcm = (TextView) findViewById(R.id.tvcm);
  56.         edtdm = (TextView) findViewById(R.id.tvdm);
  57.         edtm = (TextView) findViewById(R.id.tvm);
  58.         edkm = (TextView) findViewById(R.id.tvkm);
  59.         edinch = (TextView) findViewById(R.id.tvinch);
  60.         edft = (TextView) findViewById(R.id.tvft);
  61.         edyd = (TextView) findViewById(R.id.tvyd);
  62.         edmile = (TextView) findViewById(R.id.tvmile);
  63.         ednmile = (TextView) findViewById(R.id.tvnmile);
  64.  
  65.         Spinner SpinnerText = (Spinner) findViewById(R.id.spinner_length);
  66.         SpinnerText
  67.                 .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  68.                     public void onItemSelected(AdapterView<?> parent,
  69.                             View view, int pos, long id) {
  70.                         item = String.valueOf(parent.getItemAtPosition(pos));
  71.  
  72.                         if (item.equals("mm")) {
  73.                             edtmm.setText(kqKeyBoard * 1 + "  " + "mm");
  74.  
  75.                             edtcm.setText(kqKeyBoard * 0.1 + "  " + "cm");
  76.  
  77.                             edtdm.setText(kqKeyBoard * 0.01 + "  " + "dm");
  78.  
  79.                             edtm.setText(kqKeyBoard * 0.001 + "  " + "m");
  80.  
  81.                             edkm.setText(kqKeyBoard * 0.000001 + "  " + "km");
  82.  
  83.                             edinch.setText(kqKeyBoard * 0.03937 + "  " + "inch");
  84.  
  85.                             edft.setText(kqKeyBoard * 0.003281 + "  " + "ft");
  86.  
  87.                             edyd.setText(kqKeyBoard * 0.001094 + "  " + "yd");
  88.  
  89.                             edmile.setText(kqKeyBoard * 6.2137E-7 + "  "
  90.                                     + "mile");
  91.  
  92.                             ednmile.setText(kqKeyBoard * 5.3996E-7 + "  "
  93.                                     + "nmile");
  94.                         } else if (item.equals("cm")) {
  95.                             edtmm.setText(kqKeyBoard * 10 + "  " + "mm");
  96.  
  97.                             edtcm.setText(kqKeyBoard * 1 + "  " + "cm");
  98.  
  99.                             edtdm.setText(kqKeyBoard * 0.1 + "  " + "dm");
  100.  
  101.                             edtm.setText(kqKeyBoard * 0.01 + "  " + "m");
  102.  
  103.                             edkm.setText(kqKeyBoard * 0.00001 + "  " + "km");
  104.  
  105.                             edinch.setText(kqKeyBoard * 0.393701 + "  "
  106.                                     + "inch");
  107.  
  108.                             edft.setText(kqKeyBoard * 0.032808 + "  " + "ft");
  109.  
  110.                             edyd.setText(kqKeyBoard * 0.010936 + "  " + "yd");
  111.  
  112.                             edmile.setText(kqKeyBoard * 0.000006 + "  "
  113.                                     + "mile");
  114.  
  115.                             ednmile.setText(kqKeyBoard * 0.000005 + "  "
  116.                                     + "nmile");
  117.                         }
  118.  
  119.                     }
  120.  
  121.                     public void onNothingSelected(AdapterView<?> parent) {
  122.                     }
  123.                 });
  124.  
  125.         mKeyboard = new Keyboard(this, R.xml.keyboard);
  126.         mTargetView = (EditText) findViewById(R.id.target_length);
  127.         mtext = (EditText) findViewById(R.id.target_length);
  128.         mtext.addTextChangedListener(new TextWatcher() {
  129.  
  130.             @Override
  131.             public void onTextChanged(CharSequence s, int start, int before,
  132.                     int count) {
  133.                 // TODO Auto-generated method stub
  134.                 kqKeyBoard = Integer.parseInt(mtext.getText().toString());
  135.  
  136.    
  137.                 if (item.equals("mm")) {
  138.                     edtmm.setText(kqKeyBoard * 1 + "  " + "mm");
  139.  
  140.                     edtcm.setText(kqKeyBoard * 0.1 + "  " + "cm");
  141.  
  142.                     edtdm.setText(kqKeyBoard * 0.01 + "  " + "dm");
  143.  
  144.                     edtm.setText(kqKeyBoard * 0.001 + "  " + "m");
  145.  
  146.                     edkm.setText(kqKeyBoard * 0.000001 + "  " + "km");
  147.  
  148.                     edinch.setText(kqKeyBoard * 0.03937 + "  " + "inch");
  149.  
  150.                     edft.setText(kqKeyBoard * 0.003281 + "  " + "ft");
  151.  
  152.                     edyd.setText(kqKeyBoard * 0.001094 + "  " + "yd");
  153.  
  154.                     edmile.setText(kqKeyBoard * 6.2137E-7 + "  "
  155.                             + "mile");
  156.  
  157.                     ednmile.setText(kqKeyBoard * 5.3996E-7 + "  "
  158.                             + "nmile");
  159.                 } else if (item.equals("cm")) {
  160.                     edtmm.setText(kqKeyBoard * 10 + "  " + "mm");
  161.  
  162.                     edtcm.setText(kqKeyBoard * 1 + "  " + "cm");
  163.  
  164.                     edtdm.setText(kqKeyBoard * 0.1 + "  " + "dm");
  165.  
  166.                     edtm.setText(kqKeyBoard * 0.01 + "  " + "m");
  167.  
  168.                     edkm.setText(kqKeyBoard * 0.00001 + "  " + "km");
  169.  
  170.                     edinch.setText(kqKeyBoard * 0.393701 + "  "
  171.                             + "inch");
  172.  
  173.                     edft.setText(kqKeyBoard * 0.032808 + "  " + "ft");
  174.  
  175.                     edyd.setText(kqKeyBoard * 0.010936 + "  " + "yd");
  176.  
  177.                     edmile.setText(kqKeyBoard * 0.000006 + "  "
  178.                             + "mile");
  179.  
  180.                     ednmile.setText(kqKeyBoard * 0.000005 + "  "
  181.                             + "nmile");
  182.                 }
  183.  
  184.             }
  185.  
  186.             @Override
  187.             public void beforeTextChanged(CharSequence s, int start, int count,
  188.                     int after) {
  189.                 // TODO Auto-generated method stub
  190.  
  191.             }
  192.  
  193.             @Override
  194.             public void afterTextChanged(Editable s) {
  195.                 // TODO Auto-generated method stub
  196.                 // Toast.makeText(LengtH.this, mtext.getText().toString(),
  197.                 // Toast.LENGTH_LONG).show();
  198.             }
  199.         });
  200.  
  201.         mTargetView.setOnTouchListener(new View.OnTouchListener() {
  202.  
  203.             @Override
  204.             public boolean onTouch(View v, MotionEvent event) {
  205.                 // Dobbiamo intercettare l'evento onTouch in modo da aprire la
  206.                 // nostra tastiera e prevenire che venga aperta quella di
  207.                 // Android
  208.                 showKeyboardWithAnimation();
  209.                 toggleKeyboardVisibility();
  210.                 return true;
  211.             }
  212.         });
  213.  
  214.         mKeyboardView = (CustomKeyboardView) findViewById(R.id.keyboard_view_length);
  215.         mKeyboardView.setKeyboard(mKeyboard);
  216.  
  217.         mKeyboardView.setEnabled(true);
  218.         mKeyboardView.setPreviewEnabled(true);
  219.         mKeyboardView.setOnKeyListener(this);
  220.         mKeyboardView
  221.                 .setOnKeyboardActionListener(new BasicOnKeyboardActionListener(
  222.                         this));
  223.  
  224.     }
  225.  
  226.     private void toggleKeyboardVisibility() {
  227.         CustomKeyboardView keyboardView = (CustomKeyboardView) findViewById(R.id.keyboard_view_length);
  228.         int visibility = keyboardView.getVisibility();
  229.         switch (visibility) {
  230.         case View.VISIBLE:
  231.             keyboardView.setVisibility(View.INVISIBLE);
  232.             break;
  233.         case View.GONE:
  234.         case View.INVISIBLE:
  235.             keyboardView.setVisibility(View.VISIBLE);
  236.             break;
  237.         }
  238.     }
  239.  
  240.     private void showKeyboardWithAnimation() {
  241.         if (mKeyboardView.getVisibility() == View.GONE) {
  242.             Animation animation = AnimationUtils.loadAnimation(LengtH.this,
  243.                     R.anim.slide_in_bottom);
  244.             mKeyboardView.showWithAnimation(animation);
  245.         }
  246.     }
  247.  
  248.     @Override
  249.     public boolean onKey(View v, int keyCode, KeyEvent event) {
  250.         // TODO Auto-generated method stub
  251.  
  252.         return false;
  253.     }
  254.  
  255.     @Override
  256.     public void onKey(int primaryCode, int[] keyCodes) {
  257.         // TODO Auto-generated method stub
  258.         Log.d(TAG, "onKey? primaryCode=" + primaryCode);
  259.         Toast.makeText(this, primaryCode, Toast.LENGTH_SHORT).show();
  260.         int n1 = 0; // -1 count
  261.         for (int keyCode : keyCodes) {
  262.             if (keyCode == -1) {
  263.                 n1++;
  264.                 continue;
  265.             }
  266.             Log.v(TAG, "keyCode=" + keyCode);
  267.             Toast.makeText(this, keyCode, Toast.LENGTH_SHORT).show();
  268.         }
  269.         Log.v(TAG, "keyCode=-1 *" + n1);
  270.         Toast.makeText(this, n1, Toast.LENGTH_SHORT).show();
  271.     }
  272.  
  273.     @Override
  274.     public void onPress(int arg0) {
  275.         // TODO Auto-generated method stub
  276.  
  277.     }
  278.  
  279.     @Override
  280.     public void onRelease(int arg0) {
  281.         // TODO Auto-generated method stub
  282.  
  283.     }
  284.  
  285.     @Override
  286.     public void onText(CharSequence arg0) {
  287.         // TODO Auto-generated method stub
  288.  
  289.     }
  290.  
  291.     @Override
  292.     public void swipeDown() {
  293.         // TODO Auto-generated method stub
  294.  
  295.     }
  296.  
  297.     @Override
  298.     public void swipeLeft() {
  299.         // TODO Auto-generated method stub
  300.  
  301.     }
  302.  
  303.     @Override
  304.     public void swipeRight() {
  305.         // TODO Auto-generated method stub
  306.  
  307.     }
  308.  
  309.     @Override
  310.     public void swipeUp() {
  311.         // TODO Auto-generated method stub
  312.  
  313.     }
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement