在向 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
在我开始之前,我使用按钮向 TextView 添加了一些词,然后我尝试更改这些词,我最近使用一个简单的按钮添加到 TextView,但它不起作用
但是如果我使用的是我已经在 TextView 上设置的默认文本(这意味着我没有向 TextView 添加任何新词,只是替换了一些已经存在的词),它就可以工作,为什么会这样,我的代码有什么问题吗?,解决方案是什么?
抱歉我的英文很烂,很难用英文解释一切,希望你们都能明白我的意思
谢谢,这是我的代码
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);
}
});
}
}
public void onClick(View v) {
output.append(" ricele"); // see here is the space you are adding remove that
}
});
仅当条件匹配时才设置 onclicklistener 的代码存在问题。如果在点击处理程序中,您应该移动。像这样
//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);
}
}
});
你必须检查 onClick
到 append/replace
输出中的条件。在您的代码中,joe.setOnClickListener
或 lee.setOnClickListener
是根据条件启动的。检查以下内容:
//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);
}
}
});
在我开始之前,我使用按钮向 TextView 添加了一些词,然后我尝试更改这些词,我最近使用一个简单的按钮添加到 TextView,但它不起作用
但是如果我使用的是我已经在 TextView 上设置的默认文本(这意味着我没有向 TextView 添加任何新词,只是替换了一些已经存在的词),它就可以工作,为什么会这样,我的代码有什么问题吗?,解决方案是什么?
抱歉我的英文很烂,很难用英文解释一切,希望你们都能明白我的意思
谢谢,这是我的代码
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);
}
});
}
}
public void onClick(View v) {
output.append(" ricele"); // see here is the space you are adding remove that
}
});
仅当条件匹配时才设置 onclicklistener 的代码存在问题。如果在点击处理程序中,您应该移动。像这样
//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);
}
}
});
你必须检查 onClick
到 append/replace
输出中的条件。在您的代码中,joe.setOnClickListener
或 lee.setOnClickListener
是根据条件启动的。检查以下内容:
//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);
}
}
});