簡體   English   中英

Android:獲取生成的變量及其值?

[英]Android: Obtaining a generated variable and its values?

我正在制作一個將純文本加密然后通過電子郵件發送的應用程序。 我在“ encrypt”方法中創建的cText變量(從用戶輸入的passT和keyT創建)在該方法的末尾返回。 但是,我對如何將其合並到onCreate方法中以將其新的加密內容包含到電子郵件中感到好奇? 以下是我所擁有的,但是我只會遇到錯誤:

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();
            String EmailAdd = emailT.getText().toString();

            //This must be fixed
            //String cipherText = cText.getText().toString();


            Intent email = new Intent(Intent.ACTION_SEND);//The intent
            email.setType("plain/text");
            email.putExtra(Intent.EXTRA_EMAIL, new String[]{EmailAdd.toString()});
            email.putExtra(Intent.EXTRA_SUBJECT, "Your encrypted Password");//Subject is hard coded for convenience sake
            email.putExtra(Intent.EXTRA_TEXT, cText.toString());//Here we add encrypted Password that has just been generated
            startActivity(email);//Start the activity
        }

    });

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


ScreenNext subcipher_1 = null;
String CipherTxt = subcipher_1.encrypt(message, key);
return CipherTxt;
}


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

很感謝任何形式的幫助。

如果要在Activity多個部分中使用cText變量,則需要通過將其置於方法之外而使其成為成員變量,最好像這樣

public class ScreenNext extends Activity {

int key = 0;
static char ch;
String cText;

但是您可能會想要對其進行初始化,或者在使用它或對其應用任何方法之前檢查是否為null 將變量放在此處可為其提供類作用域,以便可以在此Activity任何位置進行訪問。 您不能真正在onCreate()再次“使用”它,因為一旦onCreate()方法完成,就不會再次調用它。 我假設您是在onClick()中談論的,該onClick()是在onCreate()設置的,但是在單擊Button時將被調用。 我對您的問題的措詞有些困惑,因此,如果這不是您想要的內容,請更清楚地解釋一下

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM