我正在制作加密器应用。 问题是我在共享中转换了 action_settings 按钮。 所以我想要的是: 每当有人在EditText中写入(例如:Hello)并单击Encrypt按钮时,该字符串存储在SharedPreferences中,并准备在WhatsApp,Telegram中共享..(使用shareactionprovider制作)。 但就我而言,如果你一个接一个地加密2个单词并不重要。 它只存储第一个。
如何更改它以便它可以共享已写入的最后一个单词?
以下是代码:
public class MainActivity extends AppCompatActivity {
Button borrar, encriptar, desencriptar;
EditText palabra;
String cambio;
TextWatcher tt = null;
int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
encriptar = (Button) findViewById(R.id.btnencriptar);
desencriptar = (Button) findViewById(R.id.btndesencriptar);
palabra = (EditText) findViewById(R.id.edittext1);
encriptar.setOnClickListener(new View.OnClickListener() { //button that encrypts
@Override
public void onClick(View v) {
String texto = palabra.getText().toString(); //string taken from edittext
texto = texto.replace("a", "9R"); //changes "a" with "9R", the same with the rest of the alphabet is below
texto = texto.replace("b", "9G");
texto = texto.replace("c", "9B");
texto = texto.replace("d", "8R");
texto = texto.replace("e", "8G");
texto = texto.replace("f", "8B");
texto = texto.replace("g", "7R");
texto = texto.replace("h", "7G");
texto = texto.replace("i", "7B");
texto = texto.replace("j", "6R");
texto = texto.replace("k", "6G");
texto = texto.replace("l", "6B");
texto = texto.replace("m", "5R");
texto = texto.replace("n", "5G");
texto = texto.replace("ñ", "5B");
texto = texto.replace("o", "4R");
texto = texto.replace("p", "4G");
texto = texto.replace("q", "4B");
texto = texto.replace("r", "3R");
texto = texto.replace("s", "3G");
texto = texto.replace("t", "3B");
texto = texto.replace("u", "2R");
texto = texto.replace("v", "2G");
texto = texto.replace("w", "2B");
texto = texto.replace("x", "1R");
texto = texto.replace("y", "1G");
texto = texto.replace("z", "1B");
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Name", texto);
editor.apply();
palabra.setText(texto);
}
});
}
public void desencriptar(View v) { //this converts 9R to a, 9G to b...
String texto2 = palabra.getText().toString();
texto2 = texto2.replace("9R", "a");
texto2= texto2.replace("9G", "b");
texto2 = texto2.replace("9B", "c");
palabra.setText(texto2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) { // here is the share button that shares the stored value
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.menu_share);
ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
// Create the share Intent
Intent shareintent = new Intent(Intent.ACTION_SEND);
shareintent.setAction(Intent.ACTION_SEND);
shareintent.setType("text/plain");
SharedPreferencespreferences=PreferenceManager.getDefaultSharedPreferences(this);//the problem maybe is here where I load the string value
String name = preferences.getString("Name", "");
shareintent.putExtra(Intent.EXTRA_TEXT, name);
mShareActionProvider.setShareIntent(shareintent);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.menu_share) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是 xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/edittext1"
android:layout_alignParentTop="true"
android:layout_marginTop="85dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ENCRIPTAR"
android:id="@+id/btnencriptar"
android:layout_below="@+id/edittext1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DESENCRIPTAR"
android:id="@+id/btndesencriptar"
android:onClick="desencriptar"
android:layout_alignTop="@+id/btnencriptar"
android:layout_alignRight="@+id/edittext1"
android:layout_alignEnd="@+id/edittext1" />
非常感谢。