出门莫恨无人随,书中车马多如簇。这篇文章主要讲述Android Studio学习路程(11)相关的知识,希望能为你提供帮助。
今天没有学习新的知识,把前面所学的复习了一下,今天做了一个简单的记账APP,实现了它的登录和界面之间的跳转的功能。下面是一些代码,我还没有做完。
1 package com.example.hp.jizhang; 2 3 import android.content.Context; 4 import android.content.Intent; 5 import android.content.SharedPreferences; 6 import android.support.v7.app.ActionBarActivity; 7 import android.os.Bundle; 8 import android.text.Editable; 9 import android.text.TextWatcher; 10 import android.util.Log; 11 import android.view.View; 12 import android.widget.CheckBox; 13 import android.widget.CompoundButton; 14 import android.widget.EditText; 15 16 public class MainActivity extends ActionBarActivity implements View.OnClickListener { 17private EditText mEtPhone; 18private EditText mEtPasswd; 19private CheckBox mCBPsd; 20private String TAG ="MainActivity"; 21private String SP_PHONE = "sp_phone"; 22private String SP_PASSWD = "sp_passwd"; 23private String SP_IS_REMEMBER_PSD = "sp_is_remember_psd"; 24private SharedPreferences sharedPreferences; 25private boolean mIsChecked=false; 26 27@Override 28protected void onCreate(Bundle savedInstanceState) { 29super.onCreate(savedInstanceState); 30setContentView(R.layout.activity_main); 31 32//初始化控件 33initUI(); 34//初始化数据 35initData(); 36} 37 38private void initData() { 39//实例化sharedPreferences对象 40if(sharedPreferences==null){ 41sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE); 42} 43//回显数据 44mEtPhone.setText(sharedPreferences.getString(SP_PHONE,"")); 45mEtPasswd.setText(sharedPreferences.getString(SP_PASSWD,"")); 46mCBPsd.setChecked(mIsChecked); 47} 48 49private void initUI() { 50//获取登录按钮 51findViewById(R.id.btn_denglu).setOnClickListener(this); 52//获取电话和密码输入框 53mEtPhone = (EditText) findViewById(R.id.et_phone); 54//文本改变之后记录电话 55mEtPhone.addTextChangedListener(new TextWatcher() { 56@Override 57public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 58 59} 60 61@Override 62public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 63 64} 65 66@Override 67public void afterTextChanged(Editable editable) { 68if(mIsChecked){ 69if(sharedPreferences==null){ 70sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE); 71} 72SharedPreferences.Editor edit = sharedPreferences.edit(); 73edit.putString(SP_PHONE, mEtPhone.getText().toString()); 74edit.commit(); 75} 76} 77}); 78mEtPasswd = (EditText) findViewById(R.id.et_passwd); 79mEtPasswd.addTextChangedListener(new TextWatcher() { 80@Override 81public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 82 83} 84 85@Override 86public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 87 88} 89 90@Override 91public void afterTextChanged(Editable editable) { 92if(mIsChecked){ 93if(sharedPreferences==null){ 94sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE); 95} 96SharedPreferences.Editor edit = sharedPreferences.edit(); 97edit.putString(SP_PASSWD, mEtPasswd.getText().toString()); 98edit.commit(); 99} 100} 101}); 102//获取多选按钮 103mCBPsd = (CheckBox) findViewById( R.id.cb_remember_psd); 104mCBPsd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 105@Override 106public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { 107Log.d(TAG, "状态为:" + isChecked); 108mIsChecked = isChecked; 109if (isChecked) { 110//实例化SharedPreferences对象 111if (sharedPreferences == null) { 112sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE); 113} 114//实例化SharedPreferences的编辑者对象 115SharedPreferences.Editor edit = sharedPreferences.edit(); 116edit.putString(SP_PHONE, mEtPhone.getText().toString()); 117edit.putString(SP_PASSWD, mEtPasswd.getText().toString()); 118edit.putBoolean(SP_IS_REMEMBER_PSD, isChecked); 119//提交 120edit.commit(); 121 122 123} 124} 125}); 126} 127//获取到登录按钮后用intent方法跳转到下一个界面 128@Override 129public void onClick(View view) { 130Intent intent = new Intent(); 131switch (view.getId()){ 132case R.id.btn_denglu: 133 134//跳转到第一个界面 135intent.setClass(getApplicationContext(),FirstActivity.class); 136break; 137} 138startActivity(intent); 139} 140 }
1 package com.example.hp.jizhang; 2 3 import android.content.Intent; 4 import android.os.Bundle; 5 import android.support.v7.app.ActionBarActivity; 6 import android.view.View; 7 8 /** 9* Created by hp on 2020/2/16. 10*/ 11 public class FirstActivity extends ActionBarActivity implements View.OnClickListener { 12protected void onCreate(Bundle savedInstanceState) { 13super.onCreate(savedInstanceState); 14 15setContentView(R.layout.activity_first); 16 17//初始化控件 18initUI(); 19} 20 21private void initUI() { 22//获取控件的id 23findViewById(R.id.tv_income_month); //获取“ 月收入” 24findViewById(R.id.tv_output_month); //获取“ 月支出” 25findViewById(R.id.tv_balance); //获取“ 余额” 26findViewById(R.id.btn_add_one).setOnClickListener(this); //获取按钮“ 记一笔” 27 28} 29 30 31@Override 32public void onClick(View view) { 33Intent intent = new Intent(); 34switch (view.getId()){ 35case R.id.btn_add_one: 36//点击“ 记一笔” 按钮跳转到第二个界面 37intent.setClass(getApplicationContext(),SecondActivity.class); 38break; 39} 40startActivity(intent); 41} 42 }
1 package com.example.hp.jizhang; 2 3 import android.content.Intent; 4 import android.os.Bundle; 5 import android.support.v7.app.ActionBarActivity; 6 import android.view.View; 7 8 /** 9* Created by hp on 2020/2/16. 10*/ 11 public class SecondActivity extends ActionBarActivity implements View.OnClickListener { 12protected void onCreate(Bundle savedInstanceState) { 13super.onCreate(savedInstanceState); 14 15setContentView(R.layout.activity_second); 16//初始化控件 17initUI(); 18} 19 20private void initUI() { 21findViewById(R.id.btn_secondActivity_return).setOnClickListener(this); 22} 23 24@Override 25public void onClick(View view) { 26Intent intent = new Intent(); 27switch (view.getId()){ 28case R.id.btn_secondActivity_return: 29//按返回按钮跳转到第一个界面 30intent.setClass(getApplicationContext(),FirstActivity.class); 31break; 32//按收入按钮跳转到第三个界面 33case R.id.btn_income: 34intent.setClass(getApplicationContext(),ThirdActivity.class); 35case R.id.btn_output: 36intent.setClass(getApplicationContext(),FouthActivity.class); 37} 38startActivity(intent); 39} 40 }
1 package com.example.hp.jizhang; 2 3 import android.content.Intent; 4 import android.os.Bundle; 5 import android.support.v7.app.ActionBarActivity; 6 import android.view.View; 7 8 import com.example.hp.jizhang.entity.User; 9 10 /** 11* Created by hp on 2020/2/16. 12*/ 13 public class ThirdActivity extends ActionBarActivity implements View.OnClickListener { 14protected void onCreate(Bundle savedInstanceState) { 15super.onCreate(savedInstanceState); 16 17setContentView(R.layout.activity_third); 18//初始化控件 19initUI(); 20//初始化数据 21initData(); 22 23} 24 25private void initData() { 26User user = new User(); 27} 28 29private void initUI() { 30findViewById(R.id.btn_thirdActivity_return).setOnClickListener(this); 31} 32@Override 33public void onClick(View view) { 34Intent intent = new Intent(); 35switch (view.getId()){ 36case R.id.btn_thirdActivity_return: 37//跳转到第二个界面 38intent.setClass(getApplicationContext(),SecondActivity.class); 39break; 40} 41startActivity(intent); 42 43} 44 }
1 package com.example.hp.jizhang; 2 3 import android.content.Intent; 4 import android.os.Bundle; 5 import android.support.v7.app.ActionBarActivity; 6 import android.view.View; 7 8 /** 9* Created by hp on 2020/2/16. 10*/ 11 public class FouthActivity extends ActionBarActivity implements View.OnClickListener { 12 13protected void onCreate(Bundle savedInstanceState) { 14super.onCreate(savedInstanceState); 15 16setContentView(R.layout.activity_fouth); 17//初始化控件 18initUI(); 19 20} 21 22private void initUI() { 23findViewById(R.id.btn_fouthActivity_return).setOnClickListener(this); 24} 25 26@Override 27public void onClick(View view) { 28Intent intent = new Intent(); 29switch (view.getId()){ 30case R.id.btn_fouthActivity_return: 31//跳转到第二个界面 32intent.setClass(getApplicationContext(),SecondActivity.class); 33break; 34} 35startActivity(intent); 36} 37 }
下面是一些布局文件:
1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:tools="http://schemas.android.com/tools" 4android:layout_width="match_parent" 5android:layout_height="match_parent" 6android:orientation="vertical" 7android:paddingBottom="@dimen/activity_vertical_margin" 8android:paddingLeft="@dimen/activity_horizontal_margin" 9android:paddingRight="@dimen/activity_horizontal_margin" 10android:paddingTop="@dimen/activity_vertical_margin" 11tools:context="com.example.hp.jizhang.MainActivity"> 12 13< EditText 14android:id="@+id/et_phone" 15android:inputType="phone" 16android:hint="电话:" 17android:layout_width="match_parent" 18android:layout_height="wrap_content" /> 19 20< EditText 21android:id="@+id/et_passwd" 22android:inputType="textPassword" 23android:hint="密码:" 24android:layout_width="match_parent" 25android:layout_height="wrap_content" /> 26< CheckBox 27android:id="@+id/cb_remember_psd" 28android:text="记住账户" 29android:layout_width="wrap_content" 30android:layout_height="wrap_content" /> 31 32< Button 33android:id="@+id/btn_denglu" 34android:text="登录" 35android:layout_width="match_parent" 36android:layout_height="wrap_content" /> 37 38 39 40 41 < /LinearLayout>
1 < ?xml version="1.0" encoding="utf-8"?> 2 < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:tools="http://schemas.android.com/tools" 4android:layout_width="match_parent" 5android:layout_height="match_parent" 6android:paddingTop="@dimen/activity_vertical_margin" 7tools:context="com.example.hp.jizhang.FirstActivity"> 8 9 10< TextView 11android:id="@+id/tv_income_month" 12android:layout_width="match_parent" 13android:layout_height="50dp" 14android:textSize="30dp" 15android:text="本月收入:3000" 16android:background="@android:color/background_light" 17android:layout_alignParentTop="true" 18android:layout_alignParentLeft="true" 19android:layout_alignParentStart="true" 20android:layout_marginTop="59dp"/> 21 22< TextView 23android:id="@+id/tv_output_month" 24android:layout_width="match_parent" 25android:layout_height="50dp" 26android:textSize="30dp" 27android:text="本月支出:2000" 28android:background="@android:color/background_light" 29android:layout_centerVertical="true" 30android:layout_alignParentLeft="true" 31android:layout_alignParentStart="true"/> 32 33< TextView 34android:id="@+id/tv_balance" 35android:layout_width="match_parent" 36android:layout_height="50dp" 37android:textSize="30dp" 38android:text="本月余额:1000" 39android:background="@android:color/background_light" 40android:layout_above="@id/btn_add_one" 41android:layout_alignParentLeft="true" 42android:layout_alignParentStart="true" 43android:layout_marginBottom="66dp"/> 44 45< Button 46android:id="@+id/btn_add_one" 47android:layout_width="match_parent" 48android:layout_height="70dp" 49android:text="+记一笔" 50android:textSize="40dp" 51android:layout_alignParentBottom="true" 52android:layout_alignParentLeft="true" 53android:layout_alignParentStart="true" 54android:layout_marginBottom="47dp"/> 55 56 < /RelativeLayout>
1 < ?xml version="1.0" encoding="utf-8"?> 2 < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:tools="http://schemas.android.com/tools" 4android:layout_width="match_parent" 5android:layout_height="match_parent" 6android:background="#000000" 7android:padding="@dimen/activity_vertical_margin" 8tools:context="com.example.hp.jizhang.SecondActivity"> 9 10 11< Button 12android:id="@+id/btn_secondActivity_return" 13android:layout_width="80dp" 14android:layout_height="50dp" 15android:text="返回" 16android:layout_alignParentTop="true" 17android:layout_alignParentLeft="true" 18android:layout_alignParentStart="true" /> 19< EditText 20android:id="@+id/et_put_money" 21android:layout_width="match_parent" 22android:layout_height="40dp" 23android:textColor="#870c0c" 24android:hint="请输入金额:" 25android:background="#9bc9e1" 26android:textSize="20dp" 27android:layout_marginTop="44dp" 28android:layout_below="@id/tv_money" 29android:layout_alignParentLeft="true" 30android:layout_alignParentStart="true" /> 31< TextView 32android:id="@+id/tv_money" 33android:layout_width="match_parent" 34android:layout_height="40dp" 35android:text="金额" 36android:textColor="#f9f6f6" 37android:textSize="20dp" 38android:layout_marginTop="35dp" 39android:layout_below="@id/btn_secondActivity_return" 40android:layout_alignParentLeft="true" 41android:layout_alignParentStart="true"/> 42 43< Button 44android:id="@+id/btn_output" 45android:layout_width="match_parent" 46android:layout_height="70dp" 47android:text="支出" 48android:textColor="#f04b4b" 49android:layout_alignParentBottom="true" 50android:layout_alignParentLeft="true" 51android:layout_alignParentStart="true" /> 52< Button 53android:id="@+id/btn_income" 54android:layout_width="match_parent" 55android:layout_height="60dp" 56android:text="收入" 57android:textColor="#6597ed" 58android:layout_above="@id/btn_output" 59android:layout_alignParentLeft="true" 60android:layout_alignParentStart="true" 61android:layout_marginBottom="45dp" /> 62 63 64 < /RelativeLayout>
1 < ?xml version="1.0" encoding="utf-8"?> 2 < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:tools="http://schemas.android.com/tools" 4android:layout_width="match_parent" 5android:layout_height="match_parent" 6android:padding="@dimen/activity_vertical_margin" 7tools:context="com.example.hp.jizhang.ThirdActivity"> 8 9< Button 10android:id="@+id/btn_thirdActivity_return" 11android:layout_width="80dp" 12android:layout_height="50dp" 13android:text="返回" 14android:layout_alignParentTop="true" 15android:layout_alignParentLeft="true" 16android:layout_alignParentStart="true" /> 17< TextView 18android:id="@+id/tv_income_all" 19android:layout_width="match_parent" 20android:layout_height="40dp" 21android:text="收入总额:" 22android:textColor="#726f6f" 23android:textSize="30dp" 24android:background="#000000" 25android:layout_below="@id/btn_thirdActivity_return" 26android:layout_alignParentRight="true" 27android:layout_alignParentEnd="true" 28android:layout_marginTop="31dp"/> 29< EditText 30android:id="@+id/et_income_money" 31android:layout_width="match_parent" 32android:layout_height="100dp" 33android:textColor="#870c0c" 34android:hint="收入金额(¥):" 35android:layout_below="@id/tv_income_all" 36android:layout_alignParentLeft="true" 37android:layout_alignParentStart="true" 38android:layout_marginTop="41dp" /> 39 40< EditText 41android:id="@+id/et_income_nature" 42android:layout_width="match_parent" 43android:layout_height="100dp" 44android:textColor="#870c0c" 45android:hint="收入类型:" 46android:layout_marginBottom="35dp" 47android:layout_alignParentBottom="true" 48android:layout_alignParentRight="true" 49android:layout_alignParentEnd="true" /> 50 51 < /RelativeLayout>
1 < ?xml version="1.0" encoding="utf-8"?> 2 < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:tools="http://schemas.android.com/tools" 4android:layout_width="match_parent" 5android:layout_height="match_parent" 6android:padding="@dimen/activity_vertical_margin" 7tools:context="com.example.hp.jizhang.FouthActivity"> 8 9< Button 10android:id="@+id/btn_fouthActivity_return" 11android:layout_width="80dp" 12android:layout_height="50dp" 13android:text="返回" 14android:layout_alignParentTop="true" 15android:layout_alignParentLeft="true" 16android:layout_alignParentStart="true" /> 17 18< TextView 19android:id="@+id/tv_output_all" 20android:layout_width="match_parent" 21android:layout_height="40dp" 22android:text="支出总额:" 23android:textColor="#726f6f" 24android:textSize="30dp" 25android:background="#000000" 26android:layout_below="@id/btn_fouthActivity_return" 27android:layout_alignParentLeft="true" 28android:layout_alignParentStart="true" 29android:layout_marginTop="71dp"/> 30< EditText 31android:id="@+id/et_output_money" 32android:layout_width="match_parent" 33android:layout_height="100dp" 34android:textColor="#870c0c" 35android:hint="支出金额(¥):" 36android:layout_below="@id/tv_output_all" 37android:layout_alignParentLeft="true" 38android:layout_alignParentStart="true" 39android:layout_marginTop="32dp" /> 40 41< EditText 42android:id="@+id/et_output_nature" 43android:layout_width="match_parent" 44android:layout_height="100dp" 45android:textColor="#870c0c" 46android:hint="支出类型:" 47android:layout_marginBottom="50dp" 48android:layout_alignParentBottom="true" 49android:layout_alignParentLeft="true" 50android:layout_alignParentStart="true" /> 51 52 53 54 55 < /RelativeLayout>
1 < ?xml version="1.0" encoding="utf-8"?> 2 < manifest xmlns:android="http://schemas.android.com/apk/res/android" 3package="com.example.hp.jizhang"> 4 5< application 6android:allowBackup="true" 7android:icon="@mipmap/ic_launcher" 8android:label="@string/app_name" 9android:supportsRtl="true" 10android:theme="@style/AppTheme"> 11< activity android:name=".MainActivity"> 12< intent-filter> 13< action android:name="android.intent.action.MAIN" /> 14 15< category android:name="android.intent.category.LAUNCHER" /> 16< /intent-filter> 17< /activity> 18< activity android:name=".FirstActivity"> 19 20< /activity> 21< activity android:name=".SecondActivity"> 22 23< /activity> 24< activity android:name=".ThirdActivity"> 25 26< /activity> 27< activity android:name=".FouthActivity"> 28 29< /activity> 30< /application> 31 32 < /manifest>
【Android Studio学习路程(11)】
推荐阅读
- 016-Android获取onenet平台上的数据
- AndroidRetrofi的基础使用教程
- call和apply区别
- 一文读懂H5/App/小程序的区别
- 使用SAP WebIDE进行SAP Cloud Platform Business Application开发
- 疫情防范APP开发
- Windows上面搭建FlutterAndroid运行环境
- 如何更改Docker sock文件的位置()
- 如何在CentOS/RHEL上构建和安装最新的cURL版本()