您的位置:首页 > 其它

安卓开发日记2——思念计数器

2014-03-08 21:23 387 查看

完整工程链接:点击打开链接

一.应用背景

最近看了一部自认为很好看的电影——彭于晏,陈意涵演的《听说》,在电影的最后,陈意涵演的轩轩告诉男主角,每次想到他的时候就会往水鸟储蓄罐里面投一个硬币。我想很多年轻人都,包括我自己,都有过这样的困惑,我到底喜不喜欢某人,或者我该不该向某人表白,而该应用的主要功能就是帮助年轻人鼓起勇气追求幸福。

二.功能介绍

用户可以在本应用中设置思念的人的姓名,电话,然后每当想念这个人,就可以点击应用中的心形图标,计数器就会加一,当计数器的值到达100的时候,就代表用户的思念已到达临界值,应用就会自动拨打设置的电话,然后你还等什么,把她约出来表白啊。

三.技术要点

1.sharedpreference用于设置姓名,计数值,电话等配置信息。
sharedPreferences= this.getSharedPreferences("ps",MODE_WORLD_READABLE);
editor = sharedPreferences.edit();
editor.putInt("mark", 1);
editor.commit();   //注意editor写入数据后需要提交


2.actionBar

设置菜单中的Item是否在actionBar中在菜单的XML文件中进行设置,ShowAsAction=“IfRoom”显示在actionBar;

3.提示框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher)
.setMessage("还未设置那个思念的人,是否前往设置")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Intent newIntent = new Intent(MainActivity.this, setting.class);
startActivityForResult(newIntent, 0);
dialog.dismiss();

}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();

}
});
builder.create().show();


4.动画
ScaleAnimation animation = new ScaleAnimation(1.0f,0.5f,1.0f,0.5f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(500);
heart.startAnimation(animation);


5.打开拨号应用
Intent intent = new Intent("android.intent.action.CALL", Uri.parse("tel:"+phone));
startActivity(intent);


四.完整代码

mainActivity:
package com.likangtang.missingcounter;

import com.likangtang.missingcounter.R.string;

import android.net.Uri;
import android.os.Bundle;
import android.R.anim;
import android.R.integer;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.drm.DrmStore.Action;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
private TextView countTextView ;
private ImageView heart;
private SharedPreferences sharedPreferences;
private Editor editor;
private int mark=0;
private ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar= this.getActionBar();
actionBar.setTitle("思念计数器");
countTextView = (TextView) findViewById(R.id.tv);
heart = (ImageView) findViewById(R.id.heart);
heart.setOnClickListener(this);
sharedPreferences= this.getSharedPreferences("ps",MODE_WORLD_READABLE);
editor = sharedPreferences.edit();
mark = sharedPreferences.getInt("mark", 0);
if(mark==0){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher)
.setMessage("还未设置那个思念的人,是否前往设置")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Intent newIntent = new Intent(MainActivity.this, setting.class);
startActivityForResult(newIntent, 0);
dialog.dismiss();

}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();

}
});
builder.create().show();
}
else{

int count = sharedPreferences.getInt("count", 0);
CharSequence temp = Integer.toString(count);
countTextView.setText(temp);

}

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode) {
case 0:{
SharedPreferences sharedPreferences_mark = this.getSharedPreferences("MARK", MODE_WORLD_READABLE);

editor.putInt("mark", 1);
editor.commit();
}

break;

default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Intent intent = new Intent(MainActivity.this, setting.class);
startActivityForResult(intent, 0);
break;
case R.id.rst:
countTextView.setText("0");
editor.putInt("count", 0);
editor.commit();
default:
break;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {
//这里是心跳动动画
ScaleAnimation animation = new ScaleAnimation(1.0f,0.5f,1.0f,0.5f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(500);
heart.startAnimation(animation);
mark= sharedPreferences.getInt("mark", 0);
int count;
count=sharedPreferences.getInt("count", 0);
switch (v.getId()) {
case R.id.heart:
if(mark!=0){
count++;
if(count==100){
count --;
SharedPreferences sharedPreferences = this.getSharedPreferences("info", MODE_WORLD_READABLE);
String name = sharedPreferences.getString("name", "李小红");
String phone = sharedPreferences.getString("phone", "18868818435");
Intent intent = new Intent("android.intent.action.CALL", Uri.parse("tel:"+phone));
startActivity(intent);
}
else{
CharSequence temp = Integer.toString(count);
countTextView.setText(temp);
editor.putInt("count", count);
editor.commit();
}
}
else{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher)
.setMessage("还未设置那个思念的人,是否前往设置")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Intent newIntent = new Intent(MainActivity.this, setting.class);
startActivityForResult(newIntent, 0);
dialog.dismiss();

}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();

}
});
builder.create().show();
}
break;

default:
break;
}

}

}


set_activity
package com.likangtang.missingcounter;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;

public class setting extends Activity {
private EditText nameEditText ;
private EditText phoneEditText ;
private String nameString="";
private String phoneString="";
private ActionBar actionBar;
private SharedPreferences sharedPreferences;
private Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
nameEditText = (EditText) findViewById(R.id.name_et);
phoneEditText = (EditText) findViewById(R.id.phone_et);
actionBar = this.getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle("设置");
sharedPreferences = this.getSharedPreferences("info", MODE_WORLD_WRITEABLE);
editor = sharedPreferences.edit();
nameString = sharedPreferences.getString("name", "");
phoneString = sharedPreferences.getString("phone", "");
nameEditText.setText(nameString);
phoneEditText.setText(phoneString);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = this.getMenuInflater();
inflater.inflate(R.menu.setting_menu, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.ok:
nameString = nameEditText.getText().toString();
phoneString = phoneEditText.getText().toString();
if(nameString.equals("")||phoneString.equals("")){
AlertDialog.Builder builder = new AlertDialog.Builder(setting.this);
builder.setIcon(R.drawable.apic2536)
.setMessage("姓名或电话号码不能为空")
.setPositiveButton("确定",new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {
arg0.dismiss();

}
});}

else{

editor.putString("name", nameString);
editor.putString("phone", phoneString);
editor.putInt("mark", 1);
editor.commit();
this.setResult(0);
this.finish();
}
break;
case android.R.id.home:
this.finish();
default:
break;
}
return super.onOptionsItemSelected(item);
}

}


五.开发后记

这个应用实在是很简单,主要目的是用来练练手,最后界面还待美化。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: