我创建了一个重置​​按钮,但想创建一个撤消按钮?

时间:2016-12-08 06:56:28

标签: android undo

我想做一个Tic Tac toe游戏,“X”,“0” 我为明文创建了一个重置​​按钮,但也想创建一个撤销按钮,它应该恢复以前的值。

这是我的代码并对其进行了编辑: 但仍然没有工作的应用程序正在停止一条消息。 不幸的是,迪帕克已经停止了。

   public class First extends AppCompatActivity {

RadioGroup jtb1;
EditText jet1, jet2, jet3, jet4, jet5, jet6, jet7, jet8, jet9;
RadioButton jrb1,jrb2;
Button jb2,undo;
final String i="O", j="X";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);

    jet1 = (EditText) findViewById(R.id.et2);
    jet3 = (EditText) findViewById(R.id.et1);
    jet2 = (EditText) findViewById(R.id.et3);
    jet4 = (EditText) findViewById(R.id.et4);
    jet5 = (EditText) findViewById(R.id.et5);
    jet6 = (EditText) findViewById(R.id.et6);
    jet7 = (EditText) findViewById(R.id.et7);
    jet8 = (EditText) findViewById(R.id.et8);
    jet9 = (EditText) findViewById(R.id.et9);
    jtb1 = (RadioGroup)findViewById(R.id.rb1);
    jrb1 = (RadioButton)findViewById(R.id.tb1);
    jrb2 = (RadioButton)findViewById(R.id.tb2);
    jb2 = (Button)findViewById(R.id.b2);
    undo = (Button)findViewById(R.id.undo);

}



public void onRadioClicked(View v) {
    final RadioButton rb = (RadioButton) v;
    if (rb.isChecked()) {
        Toast.makeText(getApplicationContext(), rb.getText().toString()+" turn", Toast.LENGTH_SHORT).show();

     /*  jb2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jet1.setText("");
                jet2.setText("");
                jet3.setText("");
                jet4.setText("");
                jet5.setText("");
                jet6.setText("");
                jet7.setText("");
                jet8.setText("");
                jet9.setText("");
                rb.setChecked(false);
            }
        }); */

        jb2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jet1.setTag(jet1.getText().toString());
                jet1.setText("");
                jet2.setTag(jet2.getText().toString());
                jet2.setText("");
                jet3.setTag(jet3.getText().toString());
                jet3.setText("");
                jet4.setTag(jet4.getText().toString());
                jet4.setText("");
                jet5.setTag(jet5.getText().toString());
                jet5.setText("");
                jet6.setTag(jet6.getText().toString());
                jet6.setText("");
                jet7.setTag(jet7.getText().toString());
                jet7.setText("");
                jet8.setTag(jet8.getText().toString());
                jet8.setText("");
                jet9.setTag(jet9.getText().toString());
                jet9.setText("");
            }
        });

        undo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jet1.setText(jet1.getTag().toString());
                jet2.setText(jet2.getTag().toString());
                jet3.setText(jet3.getTag().toString());
                jet4.setText(jet4.getTag().toString());
                jet5.setText(jet5.getTag().toString());
                jet6.setText(jet6.getTag().toString());
                jet7.setText(jet7.getTag().toString());
                jet8.setText(jet8.getTag().toString());
                jet9.setText(jet9.getTag().toString());
            }
        });



        jet1.setKeyListener(null);
        jet1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hF) {
                if (hF) {
                    jet1.callOnClick();
                }
            }
        });
        jet1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(jrb1.isChecked()) {
                    jet1.setText(i);

                }

                else{
                    jet1.setText(j);

                }

请提供撤消按钮的代码。

3 个答案:

答案 0 :(得分:1)

jb2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            jet1.setTag(jet1.getText().toString());
            jet1.setText("");
            jet2.setTag(jet2.getText().toString());
            jet2.setText("");
            jet3.setTag(jet3.getText().toString());
            jet3.setText("");
            jet4.setTag(jet4.getText().toString());
            jet4.setText("");
            jet5.setTag(jet5.getText().toString());
            jet5.setText("");
            jet6.setTag(jet6.getText().toString());
            jet6.setText("");
            jet7.setTag(jet7.getText().toString());
            jet7.setText("");
            jet8.setTag(jet8.getText().toString());
            jet8.setText("");
            jet9.setTag(jet9.getText().toString());
            jet9.setText("");
        }
    });

    undo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            jet1.setText(jet1.getTag().toString());
            jet2.setText(jet2.getTag().toString());
            jet3.setText(jet3.getTag().toString());
            jet4.setText(jet4.getTag().toString());
            jet5.setText(jet5.getTag().toString());
            jet6.setText(jet6.getTag().toString());
            jet7.setText(jet7.getTag().toString());
            jet8.setText(jet8.getTag().toString());
            jet9.setText(jet9.getTag().toString());
        }
    });

答案 1 :(得分:0)

对于撤销功能,基本上我们需要实现Momemto设计模式。 它提供此功能。保持对象状态列表,UNDO将获得状态。

https://en.wikipedia.org/wiki/Memento_pattern

http://www.journaldev.com/1734/memento-design-pattern-java

答案 2 :(得分:0)

String old1; 
String old2;
String old3; ...

jb2.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {

   old1 = jet1.getText();
   jet1.setText("");

   old2 = jet2.getText();
   jet2.setText("");

   ....
   old9 = jet9.getText();
   jet9.setText("");
   }

 undoButton.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {

   jet1.setText(old1);
   old1 ="";

   jet2.setText(old2);
   old2 =""

   ....
   jet9.getText(old9);
   old9 ="";
   }