关闭

初识Android之简单实例二

322人阅读 评论(0) 收藏 举报

自学Android已经一星期了,花了一天时间写了个简单的例子,并在自己的手机上测试了下,感觉在手机上使用还是挺顺畅的,感觉还不错,由于我的手机没去升级,现在还只能支持Android1.5的版本,所以我这个例子所用的SDK版本也是1.5的。

写这个例子也有一定缘由的,昨天买了两本我自认为比较牛的技术书籍,还送了本有关占心术的书籍,粗略的看了眼占心术的书,发现里面有一种由生日计算幸运数的方法,并提供了一些解析说明。我试着计算了下,发现挺准的(其实这些靠不靠谱不是很重要,关键是娱乐而已嘛),于是今天我便将其写成了一个小手机程序,这样一来方便计算与查看解析,二来可以熟练下这几天所学的知识。

这个例子很简单,只有三个Activity类,五个布局文件
说明:程序中所用到的一些图片没发提供了,这个例子代码的编写质量也不是很好,这里好多地方都简单化处理了,可能看起来很别扭,真正开发中这些都要避免的,代码需要不断优化。主要代码如下:包结构为:cn.com.pan;

---------------MainActivity ------------------------------

public class MainActivity extends ExpandableListActivity {
 
 private Button exitButton;
 private Button enterButton;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  //定义一个List,该List对象为一级条目提供数据
  List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
  Map<String, String> group1 = new HashMap<String, String>();
  group1.put("group", "软件简介");
  Map<String, String> group2 = new HashMap<String, String>();
  group2.put("group", "内容简介");
  Map<String, String> group3 = new HashMap<String, String>();
  group3.put("group", "使用说明");
  Map<String, String> group4 = new HashMap<String, String>();
  group4.put("group", "用户反馈");
  Map<String, String> group5 = new HashMap<String, String>();
  group5.put("group", "广告加盟");
  groups.add(group1);
  groups.add(group2);
  groups.add(group3);
  groups.add(group4);
  groups.add(group5);
  //定义一个List,该List对象为第一个一级条目提供二级条目的数据
  List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();
  Map<String, String> child1Data1 = new HashMap<String, String>();
  child1Data1.put("child", "版本:fd-1.0");
  child1.add(child1Data1);
  Map<String,String> child1Data2 = new HashMap<String,String>();
  child1Data2.put("child", "开发者:小潘学Android");
  child1.add(child1Data2);
  //定义一个List,该List对象为第二个一级条目提供二级条目的数据
  List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
  Map<String, String> child2Data = new HashMap<String, String>();
  child2Data.put("child", "幸运数:人生角色定位以及发展使命。幸运数是诸多生命" +
    "密码中最为广泛应用的一种计算方式,由1-9个数字组成(11、22、33为卓越数)" +
    "每个数字都有从低层到高层的能量,优势与劣势共存,这同时也是一条自我完善的途径。");
  child2.add(child2Data);
  //定义一个List,该List对象为第一个一级条目提供二级条目的数据
  List<Map<String, String>> child3 = new ArrayList<Map<String, String>>();
  Map<String, String> child1Data3 = new HashMap<String, String>();
  child1Data3.put("child", "点击进入,再输入自己的生日,单击测试便可。");
  child3.add(child1Data3);
  //定义一个List,该List对象为第一个一级条目提供二级条目的数据
  List<Map<String, String>> child4 = new ArrayList<Map<String, String>>();
  Map<String, String> child1Data4 = new HashMap<String, String>();
  child1Data4.put("child", "有什么疑问或建议请联系:893736334或0731-8864034");
  child4.add(child1Data4);
  //定义一个List,该List对象为第一个一级条目提供二级条目的数据
  List<Map<String, String>> child5 = new ArrayList<Map<String, String>>();
  Map<String, String> child1Data5 = new HashMap<String, String>();
  child1Data5.put("child", "有需要内置广告的客户请联系:893736334或0731-8864034");
  child5.add(child1Data5);
  //定义一个List,该List对象用来存储所有的二级条目的数据
  List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
  childs.add(child1);
  childs.add(child2);
  childs.add(child3);
  childs.add(child4);
  childs.add(child5);

    SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
    this, groups, R.layout.group, new String[] { "group" },
    new int[] { R.id.groupTo }, childs, R.layout.child,
    new String[] { "child" }, new int[] { R.id.childTo });
  //将SimpleExpandableListAdapter对象设置给当前的ExpandableListActivity
  setListAdapter(sela);
  exitButton = (Button) findViewById(R.id.exitButton);
  exitButton.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
        finish();
   }
  });
 }
  protected void onStart(){
  super.onStart();
  enterButton = (Button) findViewById(R.id.enterButton);
  enterButton.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent = new Intent();
    intent.putExtra("one", "1");
    intent.setClass(MainActivity.this, ChildActivity.class);
    MainActivity.this.startActivity(intent);
   }
  });
 }
}
--------------------ChildActivity -----------------------------
public class ChildActivity extends Activity {
 
 private TextView nextView;
 private static final int DATE_PICKER_ID = 1;
 private Button showDatePickerButton = null;
 private EditText yearText;
 private EditText monthText;
 private EditText dayText;
 private Button exitButton01 = null;
 private Button testButton = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.next);
        nextView = (TextView) findViewById(R.id.nextView);
        Intent intent = getIntent();
        String strone = intent.getStringExtra("one");
        int intOne = Integer.parseInt(strone);
        showDatePickerButton = (Button) findViewById(R.id.showDatePickerButton);
  showDatePickerButton.setOnClickListener(new ButtonListener());
  exitButton01 = (Button) findViewById(R.id.exitButton01);
  exitButton01.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    finish();
   }
  });
  testButton = (Button) findViewById(R.id.testButton);
  testButton.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    yearText = (EditText) findViewById(R.id.year);
    monthText = (EditText) findViewById(R.id.month);
    dayText = (EditText) findViewById(R.id.day);
    String yearStr = yearText.getText().toString();
    String monthStr = monthText.getText().toString();
    String dayStr = dayText.getText().toString();
    String regex1 = "([0-9]{4})";
    String regex2 = "(([0-1]{1}[0-9]{1})|([1-9]{1}))";
    String regex3 = "(([0-2]{1}[0-9]{1})|([3]{1}[0-1]{1})|([1-9]{1}))";
    if ((Pattern.compile(regex1).matcher(yearStr).matches())&&(Pattern.compile(regex2).matcher(monthStr).matches())&&(Pattern.compile(regex3).matcher(dayStr).matches())) {
     int yearInt = Integer.parseInt(yearStr);
        int monthInt = Integer.parseInt(monthStr);
        int dayInt = Integer.parseInt(dayStr);
        int sum = 0;
        sum = yearInt/1000+(yearInt00)/100+(yearInt000)/10+(yearInt000)+monthInt/10+monthInt+dayInt/10+dayInt;
        int resultData = 0;
        resultData = sum/10+sum;
        int result = 0;
        result = resultData/10+resultData;
     Intent intent = new Intent();
     intent.putExtra("one", ""+result);
     intent.setClass(ChildActivity.this, ResultActivity.class);
     ChildActivity.this.startActivity(intent);
    } else {
     Toast.makeText(ChildActivity.this,"请输入合法的生日(格式为:2011-05-08),其中要注意的是:年份要满足4位数形式,月份<=12日期<=31", Toast.LENGTH_SHORT).show();
    }
   }
  });
    }
    private class ButtonListener implements OnClickListener {
  @Override
  public void onClick(View v) {
   //此方法用于显示DatePickerDialog
   showDialog(DATE_PICKER_ID);
  }
 }
 //监听器,用户监听用户点下DatePikerDialog的set按钮时,所设置的年月日
 DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
  @Override
  public void onDateSet(DatePicker view, int year, int monthOfYear,
    int dayOfMonth) {
   yearText = (EditText) findViewById(R.id.year);
   monthText = (EditText) findViewById(R.id.month);
   dayText = (EditText) findViewById(R.id.day);
   yearText.setText(""+year);
   monthText.setText(""+(monthOfYear+1));
   dayText.setText(""+dayOfMonth);
   System.out.println(year + "-" + monthOfYear + "-" + dayOfMonth);
  }
 };

 @Override
 protected Dialog onCreateDialog(int id) {
  switch (id) {
  case DATE_PICKER_ID:
   return new DatePickerDialog(this, onDateSetListener, 2011, 04, 8);
  }
  return null;
 }
}
-----------------ResultActivity --------------------------------

public class ResultActivity extends Activity{
 
 private TextView resultView;
 private Button exitButton02;
 private Button readButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);
        resultView = (TextView) findViewById(R.id.result);
        Intent intent = getIntent();
        String strone = intent.getStringExtra("one");
        //Toast.makeText(ResultActivity.this,""+strone, Toast.LENGTH_LONG).show();
        int intOne = Integer.parseInt(strone);
        int result = intOne;
        resultView.setText(result + "");
        exitButton02 = (Button) findViewById(R.id.exitButton02);
        exitButton02.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    //Toast.makeText(ResultActivity.this, "实际-组织力-可靠-实干-诚恳-有勇气-任劳任怨-未雨绸缪-沉稳", Toast.LENGTH_LONG).show();
    finish();
   }
  });
        readButton = (Button) findViewById(R.id.readButton);
        readButton.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    String flag = resultView.getText().toString();
    if(flag.equals("1")){
     Toast.makeText(ResultActivity.this, "自我-独立-勇敢-力量-创造性-号召力-果敢-原创性-独特-进取-乐观-可信任-创意", Toast.LENGTH_LONG).show();
    }
    else if(flag.equals("2")){
     Toast.makeText(ResultActivity.this, "艺术感-耐心-宽容-善于分析-直觉力强-美感出色-配合协调-可靠-安静-和平-善解人意-优雅", Toast.LENGTH_LONG).show();
    }
    else if(flag.equals("3")){
     Toast.makeText(ResultActivity.this, "聪明-热情-想象力丰富-有创意-幽默感-充满活力-善于表达-时尚-擅社交-多才多艺-有趣-乐观-有激情-受欢迎", Toast.LENGTH_LONG).show();
    }
    else if(flag.equals("4")){
     Toast.makeText(ResultActivity.this, "实际-组织力-可靠-实干-诚恳-有勇气-任劳任怨-未雨绸缪-沉稳" +
       "做事认真-坚定-忠实-逻辑分明", Toast.LENGTH_LONG).show();
    }
    else if(flag.equals("5")){
     Toast.makeText(ResultActivity.this, "敢于冒险-博学多才-反传统-敢于颠覆-适应环境-坚持自我-智慧-充满活力-探索心-独创性强-视野宽-幽默感", Toast.LENGTH_LONG).show();
    }
    else if(flag.equals("6")){
     Toast.makeText(ResultActivity.this, "可信-忠实-爱和平-有同情心-可以承担-助人-亲和体贴-治疗他人-公正-奉献精神-荣誉感强-有担当-美的鉴赏-直觉力-负责", Toast.LENGTH_LONG).show();
    }
    else if(flag.equals("7")){
     Toast.makeText(ResultActivity.this, "精密分析力-神秘-博学-安静-有深度-求知欲强-寻找真理-内省-自学成才-智慧-直觉力-艺术气质-哲学意味-幸运", Toast.LENGTH_LONG).show();
    }
    else if(flag.equals("8")){
     Toast.makeText(ResultActivity.this, "领导力-果敢-勇敢-专注-有魄力-能屈能伸-智谋出色-商业头脑-成功-洞察力-有雄心-创造财富-援助他人", Toast.LENGTH_LONG).show();
    }
    else if(flag.equals("9")){
     Toast.makeText(ResultActivity.this, "智慧-博爱-高尚-付出爱心-热心公益-慈悲-利人-灵性" +
       "-懂得爱-激励别人-正义感-想象力丰富-慷慨-人道主义-浪漫-诚恳", Toast.LENGTH_LONG).show();
    }
   }
  });
    }
}
-----------------main.xml--------------------------

<?xml version="1.0" encoding="UTF-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:background="@drawable/main"
         >
 <!-- 添加一个ImageView控件 -->
    <ImageView
     android:id="@+id/myImageView"
     android:layout_width="50dip"
     android:layout_height="50dip"
     android:src="@drawable/welcome"
     android:layout_gravity="left"
    />
     <TextView
               android:layout_width="20dip"
               android:layout_height="20dip"
               />
 <LinearLayout android:orientation="vertical"
  android:layout_width="fill_parent" android:layout_height="fill_parent"
  android:layout_weight="2">
     <ExpandableListView android:id="@id/android:list"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:drawSelectorOnTop="false"/>
     <TextView android:id="@id/android:empty"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:text="No data"/>
      </LinearLayout>
      <LinearLayout android:orientation="vertical"
  android:layout_width="fill_parent" android:layout_height="fill_parent"
  android:layout_weight="6">
  <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" android:layout_height="fill_parent"
   android:layout_weight="1">
   <TableRow>
   <TextView android:id="@id/android:empty"
               android:layout_width="30dip"
               android:layout_height="fill_parent"
               />
  <Button
   android:id="@+id/enterButton"
      android:layout_width="90dip"
      android:layout_height="40dip"
      android:text="进入"
      android:background="@drawable/test"
      />
       <TextView android:id="@id/android:empty"
               android:layout_width="50dip"
               android:layout_height="fill_parent"
               />
      <Button
   android:id="@+id/exitButton"
      android:layout_width="90dip"
      android:layout_height="40dip"
      android:text="退出"
      android:background="@drawable/exit"
      />
      <TextView android:id="@id/android:empty"
               android:layout_width="50dip"
               android:layout_height="fill_parent"
               />
      </TableRow>
      </TableLayout>
  </LinearLayout>
 </LinearLayout>
 ----------------------next.xml-------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/main"
    >
 <LinearLayout android:orientation="vertical"
  android:layout_width="fill_parent" android:layout_height="fill_parent"
  android:layout_weight="1">
  <!-- 添加一个ImageView控件 -->
    <ImageView
     android:id="@+id/myImageView"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:src="@drawable/happy"
     android:layout_gravity="center"
    />
     <TextView android:id="@+id/nextView"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="你好,请输入或选择你的生日吧"
               android:layout_marginLeft="25dip"
               android:textSize="20sp"
               />
        <TextView
               android:layout_width="20dip"
               android:layout_height="50dip"
               />
        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" android:layout_height="fill_parent"
   android:layout_weight="1">
   <TableRow>
        <TextView
               android:layout_width="5dip"
               android:layout_height="fill_parent"
               />
        <EditText android:id="@+id/year" android:layout_width="85dip"
   android:layout_height="wrap_content"
   android:text="2011" />
        <TextView
               android:layout_width="20dip"
               android:layout_height="wrap_content"
               android:text="年"
               />
        <EditText android:id="@+id/month" android:layout_width="85dip"
   android:layout_height="wrap_content"
   android:text="05" />
        <TextView
               android:layout_width="20dip"
               android:layout_height="wrap_content"
               android:text="月"
               />
        <EditText android:id="@+id/day" android:layout_width="85dip"
   android:layout_height="wrap_content"
   android:text="08" />
        <TextView
               android:layout_width="20dip"
               android:layout_height="wrap_content"
               android:text="日"
               />
     </TableRow>
     </TableLayout>
       </LinearLayout>
      <LinearLayout android:orientation="vertical"
  android:layout_width="fill_parent" android:layout_height="fill_parent"
  android:layout_weight="2">
   <TextView
               android:layout_width="5dip"
               android:layout_height="50dip"
               />
  <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" android:layout_height="fill_parent"
   android:layout_weight="1">
   <TableRow>
   <TextView
               android:layout_width="2dip"
               android:layout_height="fill_parent"
               />
   <Button
     android:id="@+id/testButton"
        android:layout_width="80dip"
        android:layout_height="40dip"
        android:text="测测看"
        android:background="@drawable/test"
        />
       <TextView
               android:layout_width="10dip"
               android:layout_height="fill_parent"
               />
      <Button
    android:id="@+id/showDatePickerButton"
       android:layout_width="120dip"
       android:layout_height="40dip"
       android:text="时间对话框"
       android:background="@drawable/date"
       />
      <TextView
               android:layout_width="10dip"
               android:layout_height="fill_parent"
               />
                <Button
   android:id="@+id/exitButton01"
      android:layout_width="80dip"
      android:layout_height="40dip"
      android:text="返回"
      android:background="@drawable/exit"
      />
      </TableRow>
      </TableLayout>
  </LinearLayout>
</LinearLayout>
------------------------result.xml--------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout android:orientation="vertical"
  android:layout_width="fill_parent" android:layout_height="fill_parent"
  android:layout_weight="1">
  <TextView
               android:layout_width="20dip"
               android:layout_height="50dip"
               />
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" android:layout_height="fill_parent"
   android:layout_weight="1">
   <TableRow>
   <TextView
               android:layout_width="20dip"
               android:layout_height="wrap_content"
               />
    <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="嘿嘿,经测试您的幸运数为:"
               android:textSize="20sp"
               />
 <TextView 
  android:id="@+id/result"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:textSize="20sp"
     android:textColor="#aa0000"
     />
    </TableRow>
    </TableLayout>
    <!-- 添加一个ImageView控件 -->
    <ImageView
     android:id="@+id/myImageView"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:src="@drawable/common"
     android:layout_gravity="center"
    />
    </LinearLayout>
    <LinearLayout android:orientation="vertical"
  android:layout_width="fill_parent" android:layout_height="fill_parent"
  android:layout_weight="1">
  <TextView
               android:layout_width="20dip"
               android:layout_height="20dip"
               />
        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" android:layout_height="fill_parent"
   android:layout_weight="1">
   <TableRow>
      <Button
     android:id="@+id/readButton"
        android:layout_width="90dip"
        android:layout_height="40dip"
        android:layout_marginLeft="40dip"
        android:text="查看"
        android:background="@drawable/test"
        />
   <Button
     android:id="@+id/exitButton02"
        android:layout_width="90dip"
        android:layout_height="40dip"
        android:layout_marginLeft="50dip"
        android:text="返回"
        android:background="@drawable/exit"
        />
    </TableRow>
    </TableLayout>
     </LinearLayout>
</LinearLayout>
--------------------group.xml-----------------------------
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
 <TextView android:id="@+id/groupTo"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:paddingLeft="80px"
  android:paddingTop="10px"
  android:paddingBottom="10px"
  android:textSize="20sp"
  android:text="No data" />
</LinearLayout>
--------------------child.xml----------------------------
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent" android:background="#666666" >

 <TextView android:id="@+id/childTo"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:paddingLeft="35px"
  android:paddingTop="5px"
  android:paddingBottom="5px"
  android:textSize="16sp"
  android:text="No data" />
</LinearLayout>
---------------------AndroidManifest.xml------------------------------

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cn.com.pan"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  <activity android:name=".ChildActivity" >
        </activity>
        <activity android:name=".ResultActivity" >
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />

</manifest> 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15459次
    • 积分:335
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:2篇
    • 译文:0篇
    • 评论:3条
    文章存档
    最新评论