【发布时间】:2016-04-18 17:06:25
【问题描述】:
如何获取当前日期并将其设置为字符串?我的代码保存在文本字段中输入的文本并加载它,我想对日期执行相同的操作。
public class MainActivity extends Activity {
SharedPreferences sharedpreferences;
TextView name;
public static final String mypreference = "mypref";
public static final String Name = "nameKey";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (TextView) findViewById(R.id.etName);
sharedpreferences = getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
if (sharedpreferences.contains(Name)) {
name.setText(sharedpreferences.getString(Name, ""));
}
}
public void Save(View view) {
String n = name.getText().toString();
Editor editor = sharedpreferences.edit();
editor.putString(Name, n);
editor.commit();
}
public void clear(View view) {
name = (TextView) findViewById(R.id.etName);
name.setText("");
}
public void Get(View view) {
name = (TextView) findViewById(R.id.etName);
sharedpreferences = getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
if (sharedpreferences.contains(Name)) {
name.setText(sharedpreferences.getString(Name, ""));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
【问题讨论】:
-
哪个问题让你抓狂?你试过运行
new Date().toString()吗? -
我在哪里插入该代码?谢谢
-
以及我如何获得问题中询问的日期?
标签: android