1
package com.coreprojects.calculator;

// Packages
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipData;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

@SuppressLint("NewApi")
public class MainActivity extends Activity {

    /**
     * Variables. In java there are lots of type variables like C++
     * In this case we use many type of variables, like:
     * Private, String, Integer and so on.
     * We must tell computer or JVM that we store some kind of information in RAM, so RAM must give us place to store
     * 1. TextView must be private, we store information that are Text Kind.
     * 2. String must public, we store operations, like 123, 3432, 323423 and so on...
     * 3. result must be integer. we store numbers in strings to manipulate and retrieve results in integer type.
     * 4. double is like float point numbers, 1.2, 2.3445 and so on [for calculator it is nessessery]
     * ===============================================================================================================
     * @author CoreProjects
     * @category Software Engineer
     * {@link http://coreprojects.org}
     * !!!!! This all means that we used global variables, they will be accesable for every method without initializing
     */
    private TextView display, resultPlace, dropped, dropTarget, delete;
    private TextView choice1, choice2, choice3, choice4, choice5, choice6;
    String Second_op;


    /**
     * Main function and method in Android Java
     * It is like C++ int main() function, where all other classes and objects orient here.
     * When developer writes code and classes and other .java files, we encapsulate everything here
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        // Creating new onCreate method, this means that we creating main program
        super.onCreate(savedInstanceState);

        // !!!! This must be after onCreate and means to hide all status and app bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // Call in xml folder main xml file
        setContentView(R.layout.activity_main);

        /**
         * this parameter finds the TextView by ID
         * In this case these textviews are draggable.
         * we find the place and then manipulate them to be draggable.
         * display = place where touched numbers are displayed
         * calcresult = place where we show result and than manipulate to drag in delete or somewhere
         */
        display = (TextView) findViewById(R.id.display);
        delete = (TextView) findViewById(R.id.calcResult);

        // TextView Place where we can drop dragged items
        choice1 = (TextView) findViewById(R.id.gamokleba);
        choice2 = (TextView) findViewById(R.id.mimateba);
        choice3 = (TextView) findViewById(R.id.gamravleba);
        choice4 = (TextView) findViewById(R.id.gayofa);
        choice5 = (TextView) findViewById(R.id.procenti);
        choice6 = (TextView) findViewById(R.id.delete);

        // Create droppable classes to do action
        display.setOnTouchListener(new ChoiceTouchListener());
        delete.setOnTouchListener(new ChoiceTouchListener());

        // Create dragable classes to do action
        choice1.setOnDragListener(new ChoiceDragListener());
        choice2.setOnDragListener(new ChoiceDragListener());
        choice3.setOnDragListener(new ChoiceDragListener());
        choice4.setOnDragListener(new ChoiceDragListener());
        choice5.setOnDragListener(new ChoiceDragListener());
        choice6.setOnDragListener(new ChoiceDragListener());

        // Here we useing custom font family
        Typeface Face = Typeface.createFromAsset(getAssets(), "fonts/font.ttf");

        // Setting which TextView will using this font
        delete.setTypeface(Face);
        display.setTypeface(Face);
        choice1.setTypeface(Face);
        choice2.setTypeface(Face);
        choice3.setTypeface(Face);
        choice4.setTypeface(Face);
        choice5.setTypeface(Face);

    }

    /**
     * Main method
     * We using this method to make items touchable and dragable
     * Then we use them to drop an manipulate what we want
     * @author Administrator
     *
     */
    private class ChoiceDragListener implements OnDragListener {

        // onDrag Method, imagine we dragging numbers from display TextView
        @Override
        public boolean onDrag(View v, DragEvent event) {

            // Android has onDrag action types. there are some kind of action
            switch (event.getAction()) {

                // if drag stared
                case DragEvent.ACTION_DRAG_STARTED:

                break;

                // if drag entered software
                case DragEvent.ACTION_DRAG_ENTERED:

                break;

                // if drag exited, stopped or something
                case DragEvent.ACTION_DRAG_EXITED:

                break;

                // main case, if drag dropped what we do...
                case DragEvent.ACTION_DROP:

                    // handle the dragged view being dropped over a drop view
                    View view = (View) event.getLocalState();

                    // stop displaying the view where it was before it was dragged
                    // ************* view.setVisibility(View.KEEP_SCREEN_ON);

                    // view dragged item, where numbers will be dragged
                    dropTarget = (TextView) v;

                    // view dropped item, that will be dropped in drag TextView
                    dropped = (TextView) view;

                    // view result place, when make math operation, show results
                    resultPlace = (TextView) findViewById(R.id.calcResult);

                    // simple debug
                    // ****** resultPlace.setText( dropped.getText() + " " + dropTarget.getText() );

                    // if an item has already been dropped here, there will be a tag
                    Object tag = dropTarget.getTag();

                    SharedPreferences myPrefs = getSharedPreferences("myPrefs", MODE_PRIVATE);

                    // if there is already an item here, set it back visible in its
                    // original place
                    if (tag != null)
                    {

                        // the tag is the view id already dropped here
                        int existingID = (Integer) tag;

                        // set the original view visible again
                        findViewById(existingID).setVisibility(View.VISIBLE);

                    } else {

                        myPrefs.edit()
                            .putString("Second_op", (String) dropped.getText())
                            .commit();

                        Second_op = myPrefs.getString("Second_op", null);

                    }


                    int a = Integer.parseInt(dropped.getText().toString());
                    int b = Integer.parseInt(Second_op);

                    if ( dropTarget.getText().equals("+") && a != 0 && b != 0 ){
                        int res = a + b;
                        String result = Integer.toString(res);
                        resultPlace.setText( result );
                        myPrefs.edit().clear().commit();
                    } else {
                        resultPlace.setText( "Text" );
                    }



                    // ****** resultPlace.setText( dropped.getText() + " " + dropTarget.getText() + " " + Second_op );


                    // set the tag in the target view being dropped on - to the ID
                    // of the view being dropped
                    dropTarget.setTag(dropped.getId());

                    display.setText("");

                break;

                // if drag ended, we did it already successfully
                case DragEvent.ACTION_DRAG_ENDED:

                break;

                // what do default
                default:

                break;
            }
            return true;
        }
    }



    // second main method make this selected items touchable, to understand device we use them
    @SuppressLint("NewApi")
    private final class ChoiceTouchListener implements OnTouchListener {

        public boolean onTouch(View view, MotionEvent motionEvent) {

            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                /*
                 * Drag details: we only need default behavior - clip data could
                 * be set to pass data as part of drag - shadow can be tailored
                 */
                ClipData data = ClipData.newPlainText("", "");

                DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);

                // start dragging the item touched
                view.startDrag(data, shadowBuilder, view, 0);
                return true;
            } else {
                return false;
            }
        }
    }


    /**
     * Most neccessery one
     * When we touching and clicking number buttons we display 1, 2, 3, 4, 5 and so on
     * But if we want to have number like 388201, we create it by this function
     * Click digit 2 and when we also click second digit 8, it is appended and got 28
     * @param v
     */
    public void updateDisplay(View v) {

        // Local variables, we only use them in this method
        display = (TextView) findViewById(R.id.display);
        Button button1 = (Button) findViewById(R.id.button01);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                display.setText(display.getText().toString() + ((Button) view).getText());
            }
        });

        // Create new text that will be set in display TextView | local variable
        display.setText(display.getText().toString() + ((Button) v).getText());
    }

}

这是我的代码。我正在尝试创建拖放计算器。我有从 0 到 9 的数字;

1 2 3 4 5 6 7 8 9 0

单击它们,并创建像243这样的数字,它们在数学运算中触摸并拖动,例如+ 加号,然后创建第二个数字并在相同的数学运算中拖放。在这里我有问题,我试图在某处存储第一个值及其操作,因为当我删除秒值时,第一个值也更新了。我使用了SharedPreferences,但是当我得到结果然后将这个结果拖到操作中时,应用程序停止了。想通了,这就是 Pereferences 问题。我该如何解决这个问题?我想从两个数字中得到结果,然后也可以拖动结果。

230 + 330 = 560

560——新掉号?(当我拖放结果值时应用程序停止)

4

0 回答 0