我的Android应用中有一个EditText,您可以输入一些文字。我想要做的只是将第一个字符改为另一个字符(某种加密)。因此,我首先必须阅读每个角色。
我该怎么做?
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.answer);
ed1=(EditText)findViewById(R.id.answer);
String s=ed1.getText().toString();
abc(s);
}
public void abc(String s){
//get your string
String str = s;
//turn it into an array of chars
char[] strChars = str .toCharArray();
//set array at position 1 to an x
strChars[1] = 'x';
str = String.valueOf(strChars);
ed1.setText(str);
}
答案 0 :(得分:0)
这样的事情会起作用:
//get your string
String str = "whatevertextyouwanthere";
//turn it into an array of chars
char[] strChars = str .toCharArray();
//set array at position 1 to an x
//Check to make sure the length is above -
if (str.length() != 0)
{
//Check that we have a value of some sorts in the char array
if (strChars[0] != ''
{
//replace the char at place 0 with X
strChars[0] = 'x';//this might need to be 1
}
}
//turn the array back into a string
str = String.valueOf(strChars);
理想情况下,您应该将此功能集中在易于使用和可重用性的功能中。它应该返回str的值。
在你的onCreate中发布这个:
ed1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
abc(ed1.getText().toString());
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
上面只是我写的一些代码,但是应该做你需要它做的事情。您需要让听众观察编辑文本的更改。