2013-04-10 106 views
1

我目前在學習Java,並且是Android的新手。下面是我的onCreate方法,在它的最後我有兩個EditText字段,passT(純文本)和keyT(加密密鑰)。我想將這兩個參數作爲參數傳遞給我的加密方法(也在下面),並檢索結果並將其設置爲密碼EditText將參數從Activity.onCreate傳遞到其他方法

我想將該值設置爲用戶將輸入的電子郵件地址(emailT,當前沒有設置爲EditText的字段)。

public class ScreenNext extends Activity { 

    int key = 0; 
    static char ch; 

    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 

     send.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       String keyText = keyT.getText().toString(); 
       String passText = passT.getText().toString(); 
      } 

     }); 

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

回答

6

在一流水平

public class ScreenNext extends Activity { 

    int key = 0; 
    static char ch; 
    String keyText = null; 
    String passText = null; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // .... 
     send.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       keyText = keyT.getText().toString(); 
       passText = passT.getText().toString(); 
      } 
     }); 
     // ... 
    } 
    // ... 
} 
+2

Aaarrrrgggghhhhh聲明的變量。什麼是這種疾病造成的SO?我的眼睛在燃燒。我的大腦正在轉向泥漿並通過我的鼻孔泄漏出去。裏海的角魔正在用活鰻魚戳我。我的狗其實是一個狼人。任何在引用變量時鍵入「全局」一詞的人都應該被迫對基本編程進行測試。這些不是全球性的。根據定義,全局範圍的範圍和壽命等同於應用程序。這些是*類字段*或*類級變量*。它們不是全局的。他們是魔鬼的工作,應該在股份焚燒 – Simon 2013-04-10 20:02:31

+0

好吧,所以任何人閱讀上述和質疑我的理智應該知道@丹尼斯好心編輯他的答案... – Simon 2013-04-10 20:04:16

+0

感謝分配。但是,你看過我的加密方法嗎?我目前沒有任何keyText和passText變量的引用;我有其他人,因爲代碼最初是在Java IDE中編寫的。 我應該如何去實施這些新的變量來加密我的? – user2261396 2013-04-10 20:13:14