当单击不同的按钮时,如何在一个文本视图中顺序添加文本?

问题描述 投票:0回答:2

我在Android Studio中遇到问题。我有两个不同的按钮和一个textView。我想要的效果如下:

情况1:单击bt1时,在textView中显示“ A”。单击bt2时,在textView中添加了“ G”,因此textView显示为“ AG”。

情况2:单击bt1时,在textView中显示“ A”。再次单击bt1时,它消失并且textView为空。

情况3:完成情况1后,单击bt1时,将删除“ A”,以便textView显示“ G”。

非常感谢您在更改代码方面的帮助。预先感谢!

主要活动代码:

public class keyboard 
extends AppCompatActivity {

Button btn1,btn2,btn3;
TextView text;

@Override
protected void 
onCreate(Bundle 
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_keyboard);
text=(TextView)findViewById(R.id.txt);
btn1=(Button)findViewById(R.id.bt1);
btn2=(Button)findViewById(R.id.bt2);

btn1.setOnClickListener(new View.OnClickListener(){
    @Override

    public void onClick(View v){
        text.setText("A");
        text.setVisibility(text.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
    }
});

btn2.setOnClickListener(new View.OnClickListener(){
    @Override

    public void onClick(View v){
        text.setText(text.getText() + "G");
        text.setVisibility(text.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
    }
});}
android button textview onclicklistener
2个回答
0
投票
public class keyboard extends AppCompatActivity { Button btn1,btn2,btn3; TextView text; boolean isBtn1Clicked,isBtn2Clicked; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_keyboard); text=(TextView)findViewById(R.id.txt); btn1=(Button)findViewById(R.id.bt1); btn2=(Button)findViewById(R.id.bt2); isBtn1Clicked = false; isBtn2Clicked = false; btn1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ if(!isBtn1Clicked){ text.setText("A"); } else{ //remove 'A' from your TextView } //check your TextView if(text.getText().isEmpty()){ text.setVisibility.(View.INVISIBLE); }else { text.setVisibility.(View.VISIBLE); } }}); btn2.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ //same as btn1 }}); }

我更喜欢这样做:使用2个TextView,每个按钮处理1个TextView。


0
投票
btn1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ String curText = text.getText().toString(); if (curText.contains("A")) { text.setText(curText.replace("A", "")); } else { text.setText("A" + curText); } } }); btn2.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ String curText = text.getText().toString(); if (curText.contains("G")) { text.setText(curText.replace("G", "")); } else { text.setText(curText + "G"); } } });}

0
投票

声明一个数组列表

  • ArrayList btn1list = new ArrayList();
ArrayList btn2list = new ArrayList();

btn1.setOnClickListener仅在您的列表为空(!btn1list.size())时才将文本添加到列表btn1mylist.add(mystring)中>

    btn2.setOnClickListener仅在您的列表为空时才将文本添加到列表btn2mylist.add(mystring)(!btn2list.size())
  • 设置文本视图以读取数组值
  • 示例代码:
  • public class keyboard extends AppCompatActivity { Button btn1,btn2,btn3; TextView text; ArrayList<String> btn1list = new ArrayList<String>(); ArrayList<String> btn2list = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_keyboard); text=(TextView)findViewById(R.id.txt); btn1=(Button)findViewById(R.id.bt1); btn2=(Button)findViewById(R.id.bt2); btn1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ // text.setText("A"); //Check for the btn1List size if it is not empty add the value and show the text if(!bnt1List.size()){ // get the array list of items and set text } text.setVisibility(text.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE); } }); btn2.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ // text.setText(text.getText() + "G"); //Check for the btn2List size if it is not empty add the value and show the text if(!bnt2List.size()){ // get the array list of items and set text } text.setVisibility(text.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE); } });}

    © www.soinside.com 2019 - 2024. All rights reserved.