取消settext()以查看原始textview()

时间:2013-11-29 21:39:17

标签: android textview

我想点击Button2时取消button1中的settext()。我希望在textview()中看到原始文本。

    buton1 = (Button) findViewById(R.id.buton1);
    buton2 = (Button) findViewById(R.id.buton2);
    cikti  = (TextView) findViewById(R.id.cikti);

    buton1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            cikti.setText("You clicked!");

        }
    });

    buton2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        }
    });

}

3 个答案:

答案 0 :(得分:1)

试试这个

buton1 = (Button) findViewById(R.id.buton1);
    buton2 = (Button) findViewById(R.id.buton2);
    cikti  = (TextView) findViewById(R.id.cikti);
    final String originalText = cikti.getText().toString();

    buton1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            cikti.setText("You clicked!");

        }
    });

    buton2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            cikti.setText(originalText);
        }
    });

答案 1 :(得分:0)

根据我的理解,你想要的是:

buton1 = (Button) findViewById(R.id.buton1);
    buton2 = (Button) findViewById(R.id.buton2);
    cikti  = (TextView) findViewById(R.id.cikti);

    buton1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            cikti.setText("You clicked!");

        }
    });

    buton2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
          cikti.setText("");
        }
    });
}

答案 2 :(得分:0)

没有办法“取消”这样的更改,唯一的方法是保存包含所需文本的字符串并应用它:

buton1 = (Button) findViewById(R.id.buton1);
buton2 = (Button) findViewById(R.id.buton2);
cikti  = (TextView) findViewById(R.id.cikti);
String myString = "my text";
cikti.setText(myString);

buton1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        cikti.setText("You clicked!");

    }
});

buton2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        cikti.setText(myString);
    }
});

}

我假设您的代码的结构方式是使用xml文件设置字符串,在这种情况下,您可以执行此操作:

buton1 = (Button) findViewById(R.id.buton1);
buton2 = (Button) findViewById(R.id.buton2);
cikti  = (TextView) findViewById(R.id.cikti);

buton1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        cikti.setText("You clicked!");

    }
});

buton2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        cikti.setText(R.string.myString);
    }
});

}