Android Studio学习路程(11)
Posted mashuxia
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 { 17 private EditText mEtPhone; 18 private EditText mEtPasswd; 19 private CheckBox mCBPsd; 20 private String TAG ="MainActivity"; 21 private String SP_PHONE = "sp_phone"; 22 private String SP_PASSWD = "sp_passwd"; 23 private String SP_IS_REMEMBER_PSD = "sp_is_remember_psd"; 24 private SharedPreferences sharedPreferences; 25 private boolean mIsChecked=false; 26 27 @Override 28 protected void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.activity_main); 31 32 //初始化控件 33 initUI(); 34 //初始化数据 35 initData(); 36 } 37 38 private void initData() { 39 //实例化sharedPreferences对象 40 if(sharedPreferences==null){ 41 sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE); 42 } 43 //回显数据 44 mEtPhone.setText(sharedPreferences.getString(SP_PHONE,"")); 45 mEtPasswd.setText(sharedPreferences.getString(SP_PASSWD,"")); 46 mCBPsd.setChecked(mIsChecked); 47 } 48 49 private void initUI() { 50 //获取登录按钮 51 findViewById(R.id.btn_denglu).setOnClickListener(this); 52 //获取电话和密码输入框 53 mEtPhone = (EditText) findViewById(R.id.et_phone); 54 //文本改变之后记录电话 55 mEtPhone.addTextChangedListener(new TextWatcher() { 56 @Override 57 public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 58 59 } 60 61 @Override 62 public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 63 64 } 65 66 @Override 67 public void afterTextChanged(Editable editable) { 68 if(mIsChecked){ 69 if(sharedPreferences==null){ 70 sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE); 71 } 72 SharedPreferences.Editor edit = sharedPreferences.edit(); 73 edit.putString(SP_PHONE, mEtPhone.getText().toString()); 74 edit.commit(); 75 } 76 } 77 }); 78 mEtPasswd = (EditText) findViewById(R.id.et_passwd); 79 mEtPasswd.addTextChangedListener(new TextWatcher() { 80 @Override 81 public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 82 83 } 84 85 @Override 86 public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 87 88 } 89 90 @Override 91 public void afterTextChanged(Editable editable) { 92 if(mIsChecked){ 93 if(sharedPreferences==null){ 94 sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE); 95 } 96 SharedPreferences.Editor edit = sharedPreferences.edit(); 97 edit.putString(SP_PASSWD, mEtPasswd.getText().toString()); 98 edit.commit(); 99 } 100 } 101 }); 102 //获取多选按钮 103 mCBPsd = (CheckBox) findViewById( R.id.cb_remember_psd); 104 mCBPsd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 105 @Override 106 public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { 107 Log.d(TAG, "状态为:" + isChecked); 108 mIsChecked = isChecked; 109 if (isChecked) { 110 //实例化SharedPreferences对象 111 if (sharedPreferences == null) { 112 sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE); 113 } 114 //实例化SharedPreferences的编辑者对象 115 SharedPreferences.Editor edit = sharedPreferences.edit(); 116 edit.putString(SP_PHONE, mEtPhone.getText().toString()); 117 edit.putString(SP_PASSWD, mEtPasswd.getText().toString()); 118 edit.putBoolean(SP_IS_REMEMBER_PSD, isChecked); 119 //提交 120 edit.commit(); 121 122 123 } 124 } 125 }); 126 } 127 //获取到登录按钮后用intent方法跳转到下一个界面 128 @Override 129 public void onClick(View view) { 130 Intent intent = new Intent(); 131 switch (view.getId()){ 132 case R.id.btn_denglu: 133 134 //跳转到第一个界面 135 intent.setClass(getApplicationContext(),FirstActivity.class); 136 break; 137 } 138 startActivity(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 { 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 15 setContentView(R.layout.activity_first); 16 17 //初始化控件 18 initUI(); 19 } 20 21 private void initUI() { 22 //获取控件的id 23 findViewById(R.id.tv_income_month);//获取“月收入” 24 findViewById(R.id.tv_output_month);//获取“月支出” 25 findViewById(R.id.tv_balance); //获取“余额” 26 findViewById(R.id.btn_add_one).setOnClickListener(this); //获取按钮“记一笔” 27 28 } 29 30 31 @Override 32 public void onClick(View view) { 33 Intent intent = new Intent(); 34 switch (view.getId()){ 35 case R.id.btn_add_one: 36 //点击“记一笔”按钮跳转到第二个界面 37 intent.setClass(getApplicationContext(),SecondActivity.class); 38 break; 39 } 40 startActivity(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 { 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 15 setContentView(R.layout.activity_second); 16 //初始化控件 17 initUI(); 18 } 19 20 private void initUI() { 21 findViewById(R.id.btn_secondActivity_return).setOnClickListener(this); 22 } 23 24 @Override 25 public void onClick(View view) { 26 Intent intent = new Intent(); 27 switch (view.getId()){ 28 case R.id.btn_secondActivity_return: 29 //按返回按钮跳转到第一个界面 30 intent.setClass(getApplicationContext(),FirstActivity.class); 31 break; 32 //按收入按钮跳转到第三个界面 33 case R.id.btn_income: 34 intent.setClass(getApplicationContext(),ThirdActivity.class); 35 case R.id.btn_output: 36 intent.setClass(getApplicationContext(),FouthActivity.class); 37 } 38 startActivity(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 { 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 17 setContentView(R.layout.activity_third); 18 //初始化控件 19 initUI(); 20 //初始化数据 21 initData(); 22 23 } 24 25 private void initData() { 26 User user = new User(); 27 } 28 29 private void initUI() { 30 findViewById(R.id.btn_thirdActivity_return).setOnClickListener(this); 31 } 32 @Override 33 public void onClick(View view) { 34 Intent intent = new Intent(); 35 switch (view.getId()){ 36 case R.id.btn_thirdActivity_return: 37 //跳转到第二个界面 38 intent.setClass(getApplicationContext(),SecondActivity.class); 39 break; 40 } 41 startActivity(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 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 16 setContentView(R.layout.activity_fouth); 17 //初始化控件 18 initUI(); 19 20 } 21 22 private void initUI() { 23 findViewById(R.id.btn_fouthActivity_return).setOnClickListener(this); 24 } 25 26 @Override 27 public void onClick(View view) { 28 Intent intent = new Intent(); 29 switch (view.getId()){ 30 case R.id.btn_fouthActivity_return: 31 //跳转到第二个界面 32 intent.setClass(getApplicationContext(),SecondActivity.class); 33 break; 34 } 35 startActivity(intent); 36 } 37 }
下面是一些布局文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 android:paddingBottom="@dimen/activity_vertical_margin" 8 android:paddingLeft="@dimen/activity_horizontal_margin" 9 android:paddingRight="@dimen/activity_horizontal_margin" 10 android:paddingTop="@dimen/activity_vertical_margin" 11 tools:context="com.example.hp.jizhang.MainActivity"> 12 13 <EditText 14 android:id="@+id/et_phone" 15 android:inputType="phone" 16 android:hint="电话:" 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" /> 19 20 <EditText 21 android:id="@+id/et_passwd" 22 android:inputType="textPassword" 23 android:hint="密码:" 24 android:layout_width="match_parent" 25 android:layout_height="wrap_content" /> 26 <CheckBox 27 android:id="@+id/cb_remember_psd" 28 android:text="记住账户" 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" /> 31 32 <Button 33 android:id="@+id/btn_denglu" 34 android:text="登录" 35 android:layout_width="match_parent" 36 android: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" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingTop="@dimen/activity_vertical_margin" 7 tools:context="com.example.hp.jizhang.FirstActivity"> 8 9 10 <TextView 11 android:id="@+id/tv_income_month" 12 android:layout_width="match_parent" 13 android:layout_height="50dp" 14 android:textSize="30dp" 15 android:text=" 本月收入:3000" 16 android:background="@android:color/background_light" 17 android:layout_alignParentTop="true" 18 android:layout_alignParentLeft="true" 19 android:layout_alignParentStart="true" 20 android:layout_marginTop="59dp"/> 21 22 <TextView 23 android:id="@+id/tv_output_month" 24 android:layout_width="match_parent" 25 android:layout_height="50dp" 26 android:textSize="30dp" 27 android:text=" 本月支出:2000" 28 android:background="@android:color/background_light" 29 android:layout_centerVertical="true" 30 android:layout_alignParentLeft="true" 31 android:layout_alignParentStart="true"/> 32 33 <TextView 34 android:id="@+id/tv_balance" 35 android:layout_width="match_parent" 36 android:layout_height="50dp" 37 android:textSize="30dp" 38 android:text=" 本月余额:1000" 39 android:background="@android:color/background_light" 40 android:layout_above="@id/btn_add_one" 41 android:layout_alignParentLeft="true" 42 android:layout_alignParentStart="true" 43 android:layout_marginBottom="66dp"/> 44 45 <Button 46 android:id="@+id/btn_add_one" 47 android:layout_width="match_parent" 48 android:layout_height="70dp" 49 android:text=" + 记一笔" 50 android:textSize="40dp" 51 android:layout_alignParentBottom="true" 52 android:layout_alignParentLeft="true" 53 android:layout_alignParentStart="true" 54 android: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" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:background="#000000" 7 android:padding="@dimen/activity_vertical_margin" 8 tools:context="com.example.hp.jizhang.SecondActivity"> 9 10 11 <Button 12 android:id="@+id/btn_secondActivity_return" 13 android:layout_width="80dp" 14 android:layout_height="50dp" 15 android:text="返回" 16 android:layout_alignParentTop="true" 17 android:layout_alignParentLeft="true" 18 android:layout_alignParentStart="true" /> 19 <EditText 20 android:id="@+id/et_put_money" 21 android:layout_width="match_parent" 22 android:layout_height="40dp" 23 android:textColor="#870c0c" 24 android:hint="请输入金额:" 25 android:background="#9bc9e1" 26 android:textSize="20dp" 27 android:layout_marginTop="44dp" 28 android:layout_below="@id/tv_money" 29 android:layout_alignParentLeft="true" 30 android:layout_alignParentStart="true" /> 31 <TextView 32 android:id="@+id/tv_money" 33 android:layout_width="match_parent" 34 android:layout_height="40dp" 35 android:text="金额" 36 android:textColor="#f9f6f6" 37 android:textSize="20dp" 38 android:layout_marginTop="35dp" 39 android:layout_below="@id/btn_secondActivity_return" 40 android:layout_alignParentLeft="true" 41 android:layout_alignParentStart="true"/> 42 43 <Button 44 android:id="@+id/btn_output" 45 android:layout_width="match_parent" 46 android:layout_height="70dp" 47 android:text="支出" 48 android:textColor="#f04b4b" 49 android:layout_alignParentBottom="true" 50 android:layout_alignParentLeft="true" 51 android:layout_alignParentStart="true" /> 52 <Button 53 android:id="@+id/btn_income" 54 android:layout_width="match_parent" 55 android:layout_height="60dp" 56 android:text="收入" 57 android:textColor="#6597ed" 58 android:layout_above="@id/btn_output" 59 android:layout_alignParentLeft="true" 60 android:layout_alignParentStart="true" 61 android: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" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:padding="@dimen/activity_vertical_margin" 7 tools:context="com.example.hp.jizhang.ThirdActivity"> 8 9 <Button 10 android:id="@+id/btn_thirdActivity_return" 11 android:layout_width="80dp" 12 android:layout_height="50dp" 13 android:text="返回" 14 android:layout_alignParentTop="true" 15 android:layout_alignParentLeft="true" 16 android:layout_alignParentStart="true" /> 17 <TextView 18 android:id="@+id/tv_income_all" 19 android:layout_width="match_parent" 20 android:layout_height="40dp" 21 android:text="收入总额:" 22 android:textColor="#726f6f" 23 android:textSize="30dp" 24 android:background="#000000" 25 android:layout_below="@id/btn_thirdActivity_return" 26 android:layout_alignParentRight="true" 27 android:layout_alignParentEnd="true" 28 android:layout_marginTop="31dp"/> 29 <EditText 30 android:id="@+id/et_income_money" 31 android:layout_width="match_parent" 32 android:layout_height="100dp" 33 android:textColor="#870c0c" 34 android:hint="收入金额(¥):" 35 android:layout_below="@id/tv_income_all" 36 android:layout_alignParentLeft="true" 37 android:layout_alignParentStart="true" 38 android:layout_marginTop="41dp" /> 39 40 <EditText 41 android:id="@+id/et_income_nature" 42 android:layout_width="match_parent" 43 android:layout_height="100dp" 44 android:textColor="#870c0c" 45 android:hint="收入类型:" 46 android:layout_marginBottom="35dp" 47 android:layout_alignParentBottom="true" 48 android:layout_alignParentRight="true" 49 android: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" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:padding="@dimen/activity_vertical_margin" 7 tools:context="com.example.hp.jizhang.FouthActivity"> 8 9 <Button 10 android:id="@+id/btn_fouthActivity_return" 11 android:layout_width="80dp" 12 android:layout_height="50dp" 13 android:text="返回" 14 android:layout_alignParentTop="true" 15 android:layout_alignParentLeft="true" 16 android:layout_alignParentStart="true" /> 17 18 <TextView 19 android:id="@+id/tv_output_all" 20 android:layout_width="match_parent" 21 android:layout_height="40dp" 22 android:text="支出总额:" 23 android:textColor="#726f6f" 24 android:textSize="30dp" 25 android:background="#000000" 26 android:layout_below="@id/btn_fouthActivity_return" 27 android:layout_alignParentLeft="true" 28 android:layout_alignParentStart="true" 29 android:layout_marginTop="71dp"/> 30 <EditText 31 android:id="@+id/et_output_money" 32 android:layout_width="match_parent" 33 android:layout_height="100dp" 34 android:textColor="#870c0c" 35 android:hint="支出金额(¥):" 36 android:layout_below="@id/tv_output_all" 37 android:layout_alignParentLeft="true" 38 android:layout_alignParentStart="true" 39 android:layout_marginTop="32dp" /> 40 41 <EditText 42 android:id="@+id/et_output_nature" 43 android:layout_width="match_parent" 44 android:layout_height="100dp" 45 android:textColor="#870c0c" 46 android:hint="支出类型:" 47 android:layout_marginBottom="50dp" 48 android:layout_alignParentBottom="true" 49 android:layout_alignParentLeft="true" 50 android: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" 3 package="com.example.hp.jizhang"> 4 5 <application 6 android:allowBackup="true" 7 android:icon="@mipmap/ic_launcher" 8 android:label="@string/app_name" 9 android:supportsRtl="true" 10 android: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)的主要内容,如果未能解决你的问题,请参考以下文章