我有一点Java经验,而且我只是Android环境的新手。我以前在Java中创建了一个加密算法(使用简单的移位密码)。我已经进行了更改,以便它成功地适应Android代码,但是我还没有完成它的工作。计划是让用户在一个EditText字段中输入明文,在第二个字段中输入密钥,在第三个字段中输入他们的电子邮件。点击“发送”按钮后,密码文本将被发送到他们的电子邮箱。下面,我在注释中包含了Java版本的主要方法(现在在我的onCreate方法中)。
public class ScreenNext extends Activity {
int key = 0;
static char ch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_next);
EditText emailT;//Import EditTexts (Key and Email)
Button send = (Button) findViewById(R.id.bSend);//Import button1 (Send) final EditText passT = (EditText) findViewById(R.id.etTogg);//passT variable for Password Text for EditText field
final EditText keyT = (EditText) findViewById(R.id.etKey);
final EditText passT = (EditText) findViewById(R.id.etTogg);//passT variable for Password Text for EditText field
//convert the JOption Panes now into Android equivalents
//String choice = JOptionPane.showInputDialog("(1) Encrypt \n"+ "(2) Close");
//String subKey = JOptionPane.showInputDialog(null, "Enter your ideal key");
//String message = JOptionPane.showInputDialog(null, "Enter message");
//String cText = subcipher_1.message(choice, subKey, message);
//JOptionPane.showMessageDialog(null, cText);//send cText as email
}//End onCreate
public static String message(String choice, String subKey, String message) {
int Option = Integer.parseInt(choice);//Must pareseInt
int key = Integer.parseInt(subKey);
message = message.toLowerCase();
//If the key is 26, prompt the user to change the key
if (key % 26 == 0) {
//Toast.makeText(getApplicationContext(), "You can't use a modulus of 26 as a key", Toast.LENGTH_LONG).show();
}
ScreenNext subcipher_1 = null;
String CipherTxt = subcipher_1.encrypt(message, key);
return CipherTxt;
}
// Message prompt method finished, now we create the method that has the
// encrypting algorithms
public static String encrypt(String Txt, int key) {
//local var cipherText of type string is init empty
String CipherTxt = "";//May be able to remove this'un
String cText="";
//enhanced for loop
// start at 0, go until "as long as input text"
for (int i = 0; i < Txt.length(); i++) {
//get a char from the string at index i (start at 0 work through to end of string)
// and store in local var extractedChar for type char
char extractedChar = Txt.charAt(i);
/* enhanced for loop
* start at 0, go until end of user entered cipherKeyValue
* either set to lowercase a or add one to the char
* uses the checkifz method
*/
for (int j = 0; j < key; j++) {
ScreenNext subcipher_1 = null;
if (subcipher_1.checkIfZ(extractedChar) == true) {
extractedChar = 'a';
} else {
extractedChar++;
}
CipherTxt= new StringBuilder().append(extractedChar).toString();
}
//add extracted char to builder object
//change object builder to string and assing to cipherText of type String
//create new object builder from StringBuilder class
cText = cText.concat(CipherTxt);
}
//Pass the cipherText value out of the method to whom ever called it
return cText;
}
// public method properCase, makes all strings lowercase
public String properCase(String input) {
if (input.length() == 0) {
return "";
}
if (input.length() == 1) {
return input.toLowerCase();//currently set to default; will shtill compile
}
return input.substring(0).toLowerCase();//no locale set
}
//Check if the letter is Z so we can loop back to A
public static boolean checkIfZ(char cInput) {
boolean yesNo = false;
if (cInput++ == 0x7A) {
yesNo = true;
}
return yesNo;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.screen_next, menu);
return true;
}
}
当然我理解Android中不存在JOptionPane,因此我将提示用户在EditText字段中输入他们的输入。我有三个:用于明文输入的passT,用于键输入的keyT和用于用户电子邮件的emailT。
如何将EditText字段合并到加密和消息方法中的相应变量?
建议将不胜感激。
答案 0 :(得分:0)
向Send
按钮添加点击监听器,onClick
事件从字段中获取值。
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String keyText = keyT.getText().toString();
String passText = passT.getText().toString();
}
});