简体   繁体   English

在向 TextView 添加一些单词后,我尝试使用一个简单的按钮更改 java android 中字符串的某些部分,但它不起作用

[英]i try to change some parts of the String in java android using a simple button after i add some words to the TextView, but it didn't work

before i am start, i add some words using a Button to the TextView, and then i try to change that words, that i add recently to the TextView using a simple Button, but it didn't works在开始之前,我使用 Button 添加一些词到 TextView,然后我尝试更改那些词,我最近使用一个简单的 Button 添加到 TextView,但它不起作用

but if i am using default text that i already set up on the TextView(it mean i didn't add any new words to the TextView, and just replace some parts of the words that already there), it works, why is this happen, is anything wrong with my code?, and what is the solution?但是如果我使用的是我已经在 TextView 上设置的默认文本(这意味着我没有向 TextView 添加任何新单词,只是替换了已经存在的单词的某些部分),它可以工作,为什么会发生这种情况,我的代码有什么问题吗?,解决方案是什么?

sorry my english are pretty bad, it's hard to explain everything in english, hope you all understand what i mean对不起,我的英语很糟糕,很难用英语解释一切,希望你们都明白我的意思

thanks, here is my code谢谢,这是我的代码

public class MainActivity extends AppCompatActivity {

private TextView output;
private Button joe, lee, ricele;
private String outputValue;

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

    //name to be added or replace
    joe = findViewById(R.id.joe);
    lee = findViewById(R.id.lee);
    ricele = findViewById(R.id.ricele);

    //get TextView id
    output = findViewById(R.id.output);

    //get String from TextView
    outputValue = output.getText().toString();

    //to added or update the last name to the output(TextView)
    if (!outputValue.contains("ricele")) {
        joe.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View v) {
                output.append(" ricele");
            }
        });
    }

    //to replace the last name "ricele" to "lee" (but this is didn't work)
    if (outputValue.contains("ricele")) {
        lee.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                outputValue = outputValue.replace("ricele", "lee");
                output.setText(outputValue);
            }
        });
    }
}

Problem in your code that you set onclicklistener only if condition match.只有在条件匹配时才设置 onclicklistener 的代码中的问题。 You should move if inside on click handler.如果在点击处理程序里面,你应该移动。 Something like this像这样的东西

//to added or update the last name to the output(TextView)
        joe.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View v) {
                String outputValue = output.getText().toString();
                if (!outputValue.contains("ricele")) {
                    output.append(" ricele");
                }
            }
        });

    //to replace the last name "ricele" to "lee" (but this is didn't work)

        lee.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String outputValue = output.getText().toString();
                if (outputValue.contains("ricele")) {
                    outputValue = outputValue.replace("ricele", "lee");
                    output.setText(outputValue);
                }
            }
        });

You have to check the condition inside onClick to append/replace output.您必须检查onClick内的条件才能append/replace输出。 In your code either joe.setOnClickListener or lee.setOnClickListener is initiated due to condition.在您的代码中, joe.setOnClickListenerlee.setOnClickListener由于条件而启动。 Check below:检查以下:

//to added or update the last name to the output(TextView)

joe.setOnClickListener(new View.OnClickListener() {
    @SuppressLint("SetTextI18n")
    @Override
    public void onClick(View v) {
        outputValue = output.getText().toString();

        if (!outputValue.contains("ricele")) {
            output.append(" ricele");
        }
    }
});

//to replace the last name "ricele" to "lee"

lee.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        outputValue = output.getText().toString();

        if (outputValue.contains("ricele")) {
            outputValue = outputValue.replace("ricele", "lee");
            output.setText(outputValue);
        }
    }
});
 public void onClick(View v) {
            output.append(" ricele"); // see here is the space you are adding remove that
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM