2013-04-10 28 views
0

我在Java中有一點經驗,我只是對Android環境的新手。我以前在Java中創建了一個加密算法(使用簡單的移位密碼)。我已經做出了改變,以便它成功地適應Android代碼,但是我還沒有完成它的工作。計劃是讓用戶輸入純文本到一個EditText字段中,第二個是密鑰,第三個是他們的電子郵件。點擊'發送'按鈕後,密文將被髮送到他們的電子郵件。下面,我已經在Java版本中註釋了主要方法(現在在我的onCreate方法中)。Android:讓我的EditText字段借用其他方法的變量?

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; 
} 

}

當然,我明白的JOptionPane不存在Android的,所以我會提示用戶在的EditText字段中輸入他們的投入。我有三個:明文輸入的passT,輸入的密鑰keyT和用戶電子郵件的emailT。

怎樣才能將EditText字段合併到加密和消息方法中的相應變量中?

建議將不勝感激。

回答

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(); 
} 
}); 
+0

我很感激這種幫助,雖然它在這種情況下並沒有完全幫助我。我需要將我的純文本和鍵輸入工作到我的加密方法中,並使cText(我的密文)成爲輸出(我可能會首先點擊'發送'來檢查它是否工作)。 – user2261396 2013-04-10 17:36:55

+0

我沒有得到你的問題。您需要將明文和密鑰文本作爲參數傳遞給您的加密方法,然後檢索結果並在密碼EditText字段中設置值,對嗎? – asloob 2013-04-10 17:42:46

+0

是的,我想爲用戶輸入純文本和密鑰作爲參數傳入加密方法。然後我想要將結果發送到在電子郵件文本字段中輸入的電子郵件。 – user2261396 2013-04-10 18:00:29

相關問題