我试图创建一个应用程序,让用户在按下按钮后输入对话框中的任何名称,然后保存该名称,以及用户输入的所有其他名称,所以当他再次打开应用程序时,他会得到所有这些名字都在另一个之下。 当然,我不想在输入新名称后覆盖旧名称,我希望显然可以为旧名称添加新名称。 我之前一直在使用SharedPreferences,但我不知道如何制作这样的东西。 我的意思是在游戏中保存一个像高分的价值并稍后阅读它很容易,但这对我来说似乎很难。 如果我能自己解决这个问题,我真的不会寻求帮助,但我已经尝试了5个小时但没有成功。 请帮我。 提前谢谢!
编辑: 我的代码:
package cannon.gaming.mymarks;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends ActionBarActivity {
SharedPreferences subjectData;
String subjectname = "MySharedSubjects";
TextView textSubject;
ArrayList<String> lista = new ArrayList<String>();
List<String> list = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Button buttonAdd = (Button) findViewById(R.id.buttonAdd);
textSubject = (TextView) findViewById(R.id.textSubject);
subjectData = getSharedPreferences(subjectname, 0);
final String mysubjects = subjectData.getString("MySharedSubjects", "0");
File a = new File("/data/data/cannon.gaming.mymarks/shared_prefs/MySharedSubjects.xml");
if(a.exists()){
List<String> list = Arrays.asList(TextUtils.split(mysubjects, ","));
textSubject.append("\n" + list);
}else{
SharedPreferences.Editor editor = subjectData.edit();
editor.putString("MySharedSubjects", mysubjects);
editor.commit();
}
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showInputDialog();
}
});
}
protected void showInputDialog() {
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View promptView = layoutInflater.inflate(R.layout.activity_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setView(promptView);
final EditText editText = (EditText) promptView.findViewById(R.id.edittext);
// setup a dialog window
alertDialogBuilder.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
lista.add(String.valueOf(editText));
SharedPreferences.Editor editorr = subjectData.edit();
editorr.putString("MySharedSubjects", TextUtils.join(",", lista));
editorr.commit();
textSubject.append("\n" + editText.getText());
/*textSubject.append("\n" + editText.getText());
String subject = String.valueOf(editText);
SharedPreferences.Editor editorr = subjectData.edit();
editorr.putString("MySharedSubjects", subject + ",");
editorr.commit();*/
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
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.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}