我添加了EditText..where我输入一个单词例如:power。之后,我按下一个按钮,我希望每个字母都被更改......当我的意思是改变时,我的意思是这样的... A = d,B = e,C = f ..等等...我可以去做? 这就是我试过的......
public class MainActivity extends ActionBarActivity {
TextView myText, myText2, myText3, myText4;
Button myButton;
EditText myEditText1, myEditText2, myEditText3;
public String getText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myText = (TextView) findViewById(R.id.textView1);
myText2 = (TextView) findViewById(R.id.textView2);
myText3 = (TextView) findViewById(R.id.textView3);
myText4 = (TextView) findViewById(R.id.textView4);
myButton = (Button) findViewById(R.id.button1);
myEditText1 = (EditText) findViewById(R.id.editText1);
myEditText2 = (EditText) findViewById(R.id.editText2);
myEditText3 = (EditText) findViewById(R.id.editText3);
onClick();
}
public void onClick()
{
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (myEditText1.getText().toString().equals("a") || myEditText1.getText().toString().equals("A"))
{
myText.setText("X");
}
if (myEditText1.getText().toString().equals("b") || myEditText1.getText().toString().equals("B"))
{
myText.setText("Y");
}
if (myEditText1.getText().toString().equals("c") || myEditText1.getText().toString().equals("C"))
{
myText.setText("Z");
}
if (myEditText1.getText().toString().equals("d") || myEditText1.getText().toString().equals("D"))
{
myText.setText("A");
}
if (myEditText1.getText().toString().equals("e") || myEditText1.getText().toString().equals("E"))
{
myText.setText("B");
}
if (myEditText1.getText().toString().equals("f") || myEditText1.getText().toString().equals("F"))
{
myText.setText("C");
}
if (myEditText1.getText().toString().equals("g") || myEditText1.getText().toString().equals("G"))
{
myText.setText("D");
}
if (myEditText1.getText().toString().equals("h") || myEditText1.getText().toString().equals("H"))
{
myText.setText("E");
}
// EDIT TEXT 2
if (myEditText2.getText().toString().equals("a") || myEditText2.getText().toString().equals("A"))
{
myText2.setText("X");
}
if (myEditText2.getText().toString().equals("b") || myEditText2.getText().toString().equals("B"))
{
myText2.setText("Y");
}
if (myEditText2.getText().toString().equals("c") || myEditText2.getText().toString().equals("C"))
{
myText2.setText("Z");
}
if (myEditText2.getText().toString().equals("d") || myEditText2.getText().toString().equals("D"))
{
myText2.setText("A");
}
if (myEditText2.getText().toString().equals("e") || myEditText2.getText().toString().equals("E"))
{
myText2.setText("B");
}
if (myEditText2.getText().toString().equals("f") || myEditText2.getText().toString().equals("F"))
{
myText2.setText("C");
}
if (myEditText2.getText().toString().equals("g") || myEditText2.getText().toString().equals("G"))
{
myText2.setText("D");
}
if (myEditText2.getText().toString().equals("h") || myEditText2.getText().toString().equals("H"))
{
myText2.setText("E");
}
// EDIT TEXT 3
if (myEditText3.getText().toString().equals("a") || myEditText3.getText().toString().equals("A"))
{
myText3.setText("X");
}
if (myEditText3.getText().toString().equals("b") || myEditText3.getText().toString().equals("B"))
{
myText3.setText("Y");
}
if (myEditText3.getText().toString().equals("c") || myEditText3.getText().toString().equals("C"))
{
myText3.setText("Z");
}
if (myEditText3.getText().toString().equals("d") || myEditText3.getText().toString().equals("D"))
{
myText3.setText("A");
}
if (myEditText3.getText().toString().equals("e") || myEditText3.getText().toString().equals("E"))
{
myText3.setText("B");
}
if (myEditText3.getText().toString().equals("f") || myEditText3.getText().toString().equals("F"))
{
myText3.setText("C");
}
if (myEditText3.getText().toString().equals("g") || myEditText3.getText().toString().equals("G"))
{
myText3.setText("D");
}
if (myEditText3.getText().toString().equals("h") || myEditText3.getText().toString().equals("H"))
{
myText3.setText("E");
}
}
});
}
}
答案 0 :(得分:2)
以下是caesar密码实现的示例:Java, How to implement a Shift Cipher (Caesar Cipher)。只需从编辑文本中读取值并调用函数来加密文本。
在你的情况下,你应该这样做:
private void cipher(){
int shift = 2;
String msg = myEditTegt.getText();
String s = "";
int len = msg.length();
for(int x = 0; x < len; x++){
char c = (char)(msg.charAt(x) + shift);
if (c > 'z')
s += (char)(msg.charAt(x) - (26-shift));
else
s += (char)(msg.charAt(x) + shift);
}
myEditText.setText(s);
}
按下按钮时执行此方法。