免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1237 | 回复: 0
打印 上一主题 下一主题

[Android] 简单UI设计 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-05-27 14:28 |只看该作者 |倒序浏览
本帖最后由 rocox 于 2015-05-27 14:28 编辑


关键技术:

      用到了本地化控件:SharedPreferences,简单的说就是本地配置。

     四大组件:Intent

基本思路请看代码:

  Java代码:

  1. 1 import android.os.Bundle;
  2. 2 import android.app.Activity;
  3. 3 import android.content.Context;
  4. 4 import android.content.Intent;
  5. 5 import android.content.SharedPreferences;
  6. 6 import android.view.Menu;
  7. 7 import android.view.View;
  8. 8 import android.view.View.OnClickListener;
  9. 9 import android.widget.Button;
  10. 10 import android.widget.CheckBox;
  11. 11 import android.widget.EditText;
  12. 12 import android.widget.Toast;
  13. 13
  14. 14 public class MainActivity extends Activity {
  15. 15     private Button  logIn;
  16. 16     private CheckBox rember,auto;
  17. 17     private EditText name,password;
  18. 18     private SharedPreferences test;
  19. 19     private SharedPreferences.Editor editor;
  20. 20     private boolean haved;
  21. 21     @Override
  22. 22     protected void onCreate(Bundle savedInstanceState) {
  23. 23         super.onCreate(savedInstanceState);
  24. 24         setContentView(R.layout.activity_main);
  25. 25         
  26. 26         logIn= (Button) findViewById(R.id.button1);
  27. 27         rember=(CheckBox) findViewById(R.id.remberFlag);
  28. 28         auto=(CheckBox)    findViewById(R.id.autoRunFlag);
  29. 29         name=(EditText) findViewById(R.id.name);
  30. 30         password=(EditText) findViewById(R.id.password);
  31. 31         final Intent intent=new Intent(MainActivity.this,NewMainActivity.class);
  32. 32         //打开 或 创建一个名为UserData的xml文件
  33. 33         test=getSharedPreferences("UserData", Context.MODE_PRIVATE);
  34. 34         editor=test.edit();
  35. 35         
  36. 36         if(test.getBoolean("auto",false)!=false)
  37. 37         {
  38. 38             startActivity(intent);
  39. 39         }
  40. 40         
  41. 41         if(test.getBoolean("rember", false)!=false)
  42. 42             {
  43. 43                 String temp;
  44. 44                 if((temp=test.getString("name", null))!=null)
  45. 45                     name.setText(temp);
  46. 46                 if((temp=test.getString("password",null)) != null)
  47. 47                     password.setText(temp);
  48. 48                 rember.setChecked(true);
  49. 49                 haved=true;
  50. 50             }
  51. 51         
  52. 52         logIn.setOnClickListener(new View.OnClickListener() {   
  53. 53             @Override
  54. 54             public void onClick(View v) {
  55. 55                 if(haved == false )
  56. 56                 {
  57. 57                     if(rember.isChecked())
  58. 58                     {
  59. 59                         editor.putBoolean("rember", true);
  60. 60                         editor.putString("name", name.getText().toString());
  61. 61                         editor.putString("password", password.getText().toString());
  62. 62                         editor.commit();
  63. 63                     }
  64. 64                     if(auto.isChecked()==true)
  65. 65                         editor.putBoolean("auto", true);        
  66. 66                 }
  67. 67                 startActivity(intent);
  68. 68             }
  69. 69         });
  70. 70     }
  71. 71 }
  72. 复制代码
  73.   activity_main.xml

  74. 复制代码
  75. 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  76. 2     xmlns:tools="http://schemas.android.com/tools"
  77. 3     android:layout_width="match_parent"
  78. 4     android:layout_height="match_parent"
  79. 5     android:paddingBottom="@dimen/activity_vertical_margin"
  80. 6     android:paddingLeft="@dimen/activity_horizontal_margin"
  81. 7     android:paddingRight="@dimen/activity_horizontal_margin"
  82. 8     android:paddingTop="@dimen/activity_vertical_margin"
  83. 9     tools:context=".MainActivity" >
  84. 10
  85. 11     <EditText
  86. 12         android:id="@+id/name"
  87. 13         android:layout_width="match_parent"
  88. 14         android:layout_height="wrap_content"
  89. 15         android:layout_alignLeft="@+id/password"
  90. 16         android:layout_alignParentTop="true"
  91. 17         android:layout_marginTop="16dp"
  92. 18         android:ems="10" />
  93. 19
  94. 20     <EditText
  95. 21         android:id="@+id/password"
  96. 22         android:layout_width="match_parent"
  97. 23         android:layout_height="wrap_content"
  98. 24         android:layout_below="@+id/name"
  99. 25         android:layout_centerHorizontal="true"
  100. 26         android:layout_marginTop="18dp"
  101. 27         android:ems="10" >
  102. 28
  103. 29         <requestFocus />
  104. 30     </EditText>
  105. 31
  106. 32     <Button
  107. 33         android:id="@+id/button1"
  108. 34         android:layout_width="wrap_content"
  109. 35         android:layout_height="wrap_content"
  110. 36         android:layout_alignLeft="@+id/password"
  111. 37         android:layout_below="@+id/password"
  112. 38         android:layout_marginLeft="16dp"
  113. 39         android:text="登陆" />
  114. 40
  115. 41     <CheckBox
  116. 42         android:id="@+id/autoRunFlag"
  117. 43         android:layout_width="wrap_content"
  118. 44         android:layout_height="wrap_content"
  119. 45         android:layout_alignLeft="@+id/password"
  120. 46         android:layout_below="@+id/button1"
  121. 47         android:text="自动登陆" />
  122. 48
  123. 49     <CheckBox
  124. 50         android:id="@+id/remberFlag"
  125. 51         android:layout_width="wrap_content"
  126. 52         android:layout_height="wrap_content"
  127. 53         android:layout_alignBaseline="@+id/autoRunFlag"
  128. 54         android:layout_alignBottom="@+id/autoRunFlag"
  129. 55         android:layout_alignRight="@+id/password"
  130. 56         android:text="记住密码" />
  131. 57
  132. 58 </RelativeLayout>
  133. 复制代码
  134. activity_second.xml:

  135. 复制代码
  136. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  137.     xmlns:tools="http://schemas.android.com/tools"
  138.     android:layout_width="match_parent"
  139.     android:layout_height="match_parent"
  140.     android:paddingBottom="@dimen/activity_vertical_margin"
  141.     android:paddingLeft="@dimen/activity_horizontal_margin"
  142.     android:paddingRight="@dimen/activity_horizontal_margin"
  143.     android:paddingTop="@dimen/activity_vertical_margin"
  144.     tools:context=".MainActivity" >

  145.     <RatingBar
  146.         android:id="@+id/ratingBar1"
  147.         android:layout_width="wrap_content"
  148.         android:layout_height="wrap_content"
  149.         android:layout_alignParentTop="true"
  150.         android:layout_centerHorizontal="true"
  151.         android:layout_marginTop="170dp" />

  152. </RelativeLayout>
复制代码
AndroidMainifest中要注册:
  1. <activity
  2.             android:name="com.example.log_in.NewMainActivity"
  3.             android:label="second_activity" >
  4.         </activity>
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP