安卓开发十——调试设置应用图标和源代码
我们要设置图标和名称只需要修改这三个值就就可以了
android:icon="@drawable/appimag"
android:label="记账本"
android:roundIcon="@drawable/appimag"
然后最后的效果是这样的
源代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context=".MainActivity"> <include layout="@layout/list_name" /> <View android:layout_width="fill_parent" android:layout_marginTop="0dp" android:layout_height="1dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="#aa000000" /> <ListView android:id="@+id/list_view" android:layout_width="wrap_content" android:layout_height="650dp" /> <View android:layout_width="fill_parent" android:layout_marginTop="10dp" android:layout_height="1dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="#aa000000" /> <include layout="@layout/total"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="left" android:singleLine="true" android:textSize="40sp" android:ellipsize="marquee" android:text="添加新条目" /> <Space android:layout_width="wrap_content" android:layout_height="30dp"/> <View android:layout_width="fill_parent" android:layout_marginTop="0dp" android:layout_height="1dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="#aa000000" /> <Space android:layout_width="wrap_content" android:layout_height="20dp"/> <Spinner android:id="@+id/spin" android:layout_marginLeft="3sp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Space android:layout_width="wrap_content" android:layout_height="20dp"/> <EditText android:id="@+id/et_cost_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="4dp" android:hint="事项说明" android:textColor="#ffbd27" /> <Space android:layout_width="wrap_content" android:layout_height="10dp"/> <EditText android:id="@+id/et_cost_money" android:inputType="number|numberDecimal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="4dp" android:hint="金额(元)" android:textColor="#ffbd27" /> <Space android:layout_width="wrap_content" android:layout_height="20dp"/> <DatePicker android:id="@+id/dp_cost_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:datePickerMode="spinner" android:calendarViewShown="false" /> <Space android:layout_width="wrap_content" android:layout_height="50dp"/> <Button android:onClick="okButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="确认" android:textSize="20dp" android:textColor="#333333" android:background="#ffbd27" android:layout_marginLeft="40dp" android:layout_marginRight="40dp" /> <Space android:layout_width="wrap_content" android:layout_height="20dp"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="30dp"> <TextView android:id="@+id/tv_spin" android:layout_width="180dp" android:layout_height="30dp" android:layout_marginLeft="-60dp" android:layout_alignParentLeft="true" android:gravity="center" android:textSize="15sp" android:text="Se" /> <TextView android:id="@+id/tv_title" android:layout_width="130dp" android:layout_height="30dp" android:layout_marginLeft="55dp" android:layout_alignParentLeft="true" android:gravity="center" android:textSize="15sp" android:ellipsize="marquee" android:text="costTitle" /> <TextView android:id="@+id/tv_date" android:layout_width="wrap_content" android:layout_height="30dp" android:gravity="center" android:textSize="15sp" android:layout_marginLeft="20dp" android:layout_toRightOf="@+id/tv_title" android:text="2020-05-31"/> <TextView android:id="@+id/tv_money" android:layout_width="wrap_content" android:layout_height="30dp" android:gravity="center" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:textSize="15sp" android:textColor="#ffbd27" android:text="25"/> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="80dp" android:id="@+id/month" android:layout_marginLeft="20dp" android:layout_alignParentLeft="true" android:gravity="center" android:textSize="30sp" android:text="costTitle"> </TextView>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:background="#faebd7"> <TextView android:layout_width="180dp" android:layout_height="50dp" android:layout_marginLeft="-60dp" android:layout_alignParentLeft="true" android:gravity="center" android:singleLine="true" android:textSize="25sp" android:ellipsize="marquee" android:text="收支" /> <TextView android:id="@+id/tv_title" android:layout_width="180dp" android:layout_height="50dp" android:layout_marginLeft="30dp" android:layout_alignParentLeft="true" android:gravity="center" android:singleLine="true" android:textSize="25sp" android:ellipsize="marquee" android:text="细则" /> <TextView android:id="@+id/tv_date" android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="center" android:textSize="25sp" android:layout_marginLeft="5dp" android:layout_toRightOf="@+id/tv_title" android:text="日期"/> <TextView android:id="@+id/tv_money" android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="center" android:layout_alignParentRight="true" android:layout_marginRight="8dp" android:textSize="25sp" android:text="金额"/> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <TextView android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="40sp" android:text="@string/month" /> <View android:layout_width="fill_parent" android:layout_marginTop="10dp" android:layout_height="1dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="#aa000000" /> <ListView android:id="@+id/l_month" android:layout_width="match_parent" android:layout_height="560dp" /> <ImageButton android:id="@+id/add" android:onClick="addAccount" android:layout_width="120dp" android:layout_height="120dp" android:scaleType="centerInside" android:layout_centerHorizontal="true" android:background="#00FF0000" android:src="@drawable/img" android:layout_below="@+id/list_view" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/text" android:layout_marginLeft="-30sp" android:singleLine="true" android:textSize="25sp" android:text=""> </TextView>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="50dp"> <TextView android:layout_width="180dp" android:layout_height="50dp" android:gravity="left" android:textSize="25sp" android:text=" 总计:" /> <TextView android:id="@+id/t_money" android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="right|end" android:textSize="25sp" android:textColor="#ff0000" android:text=""/> <TextView android:layout_width="180dp" android:layout_height="50dp" android:gravity="left" android:textSize="25sp" android:text="(元)" /> </LinearLayout>
package com.example.myapplication; public class costList { private String _id; private String se; private String Title; private String Date; private String Money; public String getMoney() { return Money; } public void setMoney(String money) { Money = money; } public String getDate() { return Date; } public void setDate(String date) { Date = date; } public String getTitle() { return Title; } public void setTitle(String title) { Title = title; } public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getSe() { return se; } public void setSe(String se) { this.se = se; } }
package com.example.myapplication; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper { private static int DB_VERSION = 1; private static String DB_NAME = "account_daily2.db"; public DBHelper(Context context) { super(context, DB_NAME ,null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String sql="create table account2(_id integer primary key autoincrement," +//主键 "Se varchar(20),"+// "Title varchar(20)," +//Title "Date varchar(20)," +//Date "Money vaechar(20))";//Money db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } }
package com.example.myapplication; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; public class ListAdapter extends BaseAdapter { List<costList> mList; public ListAdapter(List<costList>list) { mList=list; } @Override public int getCount() { return mList.size(); } @Override public Object getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return Long.parseLong(mList.get(position).get_id()); } @Override public View getView(int position, View convertView, ViewGroup parent) { View view=mLayoutInflater.inflate(R.layout.list_item,null); //取出数据赋值 costList item=mList.get(position); TextView tv_spin=view.findViewById(R.id.tv_spin); TextView tv_title=view.findViewById(R.id.tv_title); TextView tv_date=view.findViewById(R.id.tv_date); TextView tv_money=view.findViewById(R.id.tv_money); //绑定 tv_spin.setText(mList.get(position).getSe()); tv_title.setText(mList.get(position).getTitle()); tv_date.setText(mList.get(position).getDate()); tv_money.setText(mList.get(position).getMoney()); return view; } private List<costList>getmList; private LayoutInflater mLayoutInflater; public ListAdapter(Context context, List<costList>list) { mList=list; //通过外部传来的Context初始化LayoutInflater对象 mLayoutInflater=LayoutInflater.from(context); } }
package com.example.myapplication; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ListAdapter listAdapter; private String month11; private DBHelper helper; private ListView listView; private List<costList>list; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = getIntent(); month11 = intent.getStringExtra("date"); initView(); initData(); listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { new AlertDialog.Builder(MainActivity.this).setTitle("注意") .setMessage("是否删除要此项记录?").setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String view1= String.valueOf(listAdapter.getItemId(position)); SQLiteDatabase db = helper.getWritableDatabase(); db.delete("account2","_id= ?",new String[]{view1}); Toast.makeText(getBaseContext(), "已删除", Toast.LENGTH_SHORT).show(); initData(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), "已取消删除", Toast.LENGTH_SHORT).show(); } }).create().show(); return false; } }); } @SuppressLint("Range") private void initData() { list=new ArrayList<>(); SQLiteDatabase db=helper.getReadableDatabase(); Cursor cursor=db.query("account2",null,"Date like '"+month11+"%'",null,null, null,null); BigDecimal sum=new BigDecimal(0); while (cursor.moveToNext()){ costList clist=new costList();//构造实例 BigDecimal temp=new BigDecimal(cursor.getString(cursor.getColumnIndex("Money"))); sum=sum.add(temp); clist.setSe(cursor.getString(cursor.getColumnIndex("Se"))); clist.set_id(cursor.getString(cursor.getColumnIndex("_id"))); clist.setTitle(cursor.getString(cursor.getColumnIndex("Title"))); clist.setDate(cursor.getString(cursor.getColumnIndex("Date"))); clist.setMoney(temp.toString()); list.add(clist); } //绑定适配器 listAdapter=new ListAdapter(this,list); listView.setAdapter(listAdapter); textView.setText(sum.toString()); db.close(); } private void initView() { helper=new DBHelper(MainActivity.this); listView = findViewById(R.id.list_view); textView =findViewById(R.id.t_money); } }
package com.example.myapplication; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; public class MListAdapter extends BaseAdapter { List<costList> mList; public MListAdapter(List<costList>list) { mList=list; } @Override public int getCount() { return mList.size(); } @Override public Object getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view=mLayoutInflater.inflate(R.layout.list_month,null); //取出数据赋值 costList item=mList.get(position); TextView month=view.findViewById(R.id.month); //绑定 month.setText(mList.get(position).getDate().substring(0,4)); return view; } private List<costList>getmList; private LayoutInflater mLayoutInflater; public MListAdapter(Context context, List<costList>list) { mList=list; //通过外部传来的Context初始化LayoutInflater对象 mLayoutInflater=LayoutInflater.from(context); } }
package com.example.myapplication; import android.annotation.SuppressLint; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.os.Message; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ImageButton; import android.widget.ListView; import android.widget.AdapterView; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import java.util.ArrayList; import java.util.List; import android.os.Handler; public class monthActivity extends AppCompatActivity { List<String> list; private ImageButton Add; private DBHelper helper; private ListView listView; private Runnable mRunnable = new Runnable() { @Override public void run() { while(true){ try { //sleep2秒,可根据需求更换为响应的时间 Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } mHandler.sendMessage(mHandler.obtainMessage()); } } }; //调用定时刷新函数 private Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { refresh();//编写的定时刷新函数 } }; private void refresh() { iniView(); } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.month); Add=findViewById(R.id.add); listView=findViewById(R.id.l_month); iniView(); new Thread(mRunnable).start(); } @SuppressLint("Range") private void iniView() { helper=new DBHelper(monthActivity.this); list=new ArrayList<>(); SQLiteDatabase db=helper.getReadableDatabase(); Cursor cursor=db.query("account2",null,null,null,null, null,null); while (cursor.moveToNext()){ String mon=cursor.getString(cursor.getColumnIndex("Date")).substring(0,7); if(!find(mon)) { list.add(mon); } } db.close(); ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.list_month,list); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String itemDate=adapter.getItem(position); Intent intent=new Intent(monthActivity.this,MainActivity.class); intent.putExtra("date",itemDate); startActivity(intent); } }); } public void addAccount(View view){//跳转 Intent intent=new Intent(monthActivity.this,new_cost.class); //noinspection deprecation startActivityForResult(intent,1); } public boolean find(String a) { boolean flag=false; for (String str : list) { if(str.equals(a)) { flag=true; break; } } return flag; } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==1&&resultCode==1) { iniView(); } } }
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.ContentValues; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.text.Spanned; import java.util.ArrayList; import java.util.List; import java.util.regex.*; import android.view.Gravity; import android.view.View; import android.widget.ArrayAdapter; import android.widget.DatePicker; import android.widget.EditText; import android.widget.Spinner; import android.widget.Toast; import java.text.SimpleDateFormat; import java.util.Date; public class new_cost extends AppCompatActivity { private DBHelper helper; private Spinner et_cost_se; private EditText et_cost_title; private EditText et_cost_money; private DatePicker dp_cost_date; private String[] list=new String[]{"收入","支出"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new_cost); initView(); } private void initView() { helper = new DBHelper(new_cost.this); et_cost_se=findViewById(R.id.spin); et_cost_title = findViewById(R.id.et_cost_title); et_cost_money = findViewById(R.id.et_cost_money); dp_cost_date = findViewById(R.id.dp_cost_date); Spinner sp=findViewById(R.id.spin); ArrayAdapter<String> adapter=new ArrayAdapter<>(this,R.layout.text,list); sp.setAdapter(adapter);//下拉框文本适配 } public void okButton(View view) { String seStr=et_cost_se.getSelectedItem().toString(); String titleStr = et_cost_title.getText().toString().trim(); String moneyStr = et_cost_money.getText().toString().trim(); String dateStr = dp_cost_date.getYear() + "-" + (dp_cost_date.getMonth() + 1) + "-" + dp_cost_date.getDayOfMonth();//这里getMonth会比当前月份少一个月,所以要+1 if ("".equals(moneyStr)) {//可以不填写Title但是不能不填金额 Toast toast = Toast.makeText(this, "请填写金额", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } else { if(seStr.equals("收入")) { StringBuilder sb = new StringBuilder(moneyStr); sb.insert(0, "+"); moneyStr=sb.toString(); } else { StringBuilder sb = new StringBuilder(moneyStr); sb.insert(0, "-"); moneyStr=sb.toString(); } String[] strArr1=dateStr.split("-"); if(strArr1[1].length()==1)//保证日期格式对齐 { StringBuilder sb = new StringBuilder(strArr1[1]); sb.insert(0, "0"); dateStr=strArr1[0]+"-"+sb.toString(); } StringBuilder sb = new StringBuilder(strArr1[2]); if(strArr1[2].length()==1) { sb.insert(0, "0"); } dateStr=dateStr+"-"+sb.toString(); SQLiteDatabase db = helper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("Se",seStr); values.put("Title", titleStr); values.put("Money", moneyStr); values.put("Date", dateStr); long account = db.insert("account2", null, values); if (account > 0) { Toast toast = Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); setResult(1); finish(); } else { Toast toast = Toast.makeText(this, "请重试", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); db.close(); } setResult(1); finish(); } } }