• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Android开发实验参考代码

武飞扬头像
Moon_Quakes.
帮助1

实验三

布局文件:星级评分条

  1.  
     <?xml version="1.0" encoding="utf-8"?>
  2.  
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
         android:layout_width="match_parent"
  4.  
         android:layout_height="match_parent"
  5.  
         android:orientation="vertical" >
  6.  
     
  7.  
         <RatingBar
  8.  
             android:id="@ id/ratingBar1"
  9.  
             android:layout_width="wrap_content"
  10.  
             android:layout_height="wrap_content"
  11.  
             android:isIndicator="false"
  12.  
             android:numStars="5"
  13.  
             android:rating="3"
  14.  
             android:stepSize="0.5" />
  15.  
         
  16.  
         <Button
  17.  
             android:id="@ id/button1"
  18.  
             android:layout_width="wrap_content"
  19.  
             android:layout_height="wrap_content"
  20.  
             android:text="提交"
  21.  
             android:onClick="tijiao"/>
  22.  
         
  23.  
         <TextView
  24.  
             android:layout_width="wrap_content"
  25.  
             android:layout_height="50dp"
  26.  
             android:text="" />
  27.  
         
  28.  
         <TextView
  29.  
             android:id="@ id/textView1"
  30.  
             android:layout_width="wrap_content"
  31.  
             android:layout_height="wrap_content"
  32.  
             android:layout_gravity="center"
  33.  
             android:singleLine="false"
  34.  
             android:text="此处显示星级评分条信息"
  35.  
             android:textSize="20sp" />
  36.  
     
  37.  
     </LinearLayout>
学新通

源文件:

  1.  
    package com.example.zhangzhipeng3_3;
  2.  
     
  3.  
     import android.os.Bundle;
  4.  
     import android.app.Activity;
  5.  
     import android.view.Menu;
  6.  
     import android.view.View;
  7.  
     import android.widget.RatingBar;
  8.  
     import android.widget.TextView;
  9.  
     
  10.  
     public class MainActivity extends Activity {
  11.  
     
  12.  
     
  13.  
      RatingBar bar;
  14.  
      TextView tv;
  15.  
      int alltar;
  16.  
      @Override
  17.  
      protected void onCreate(Bundle savedInstanceState) {
  18.  
      super.onCreate(savedInstanceState);
  19.  
      setContentView(R.layout.ratingbar);
  20.  
      }
  21.  
     
  22.  
      public void tijiao(View v){
  23.  
      bar = (RatingBar) findViewById(R.id.ratingBar1);
  24.  
      tv = (TextView)findViewById(R.id.textView1);
  25.  
     
  26.  
      int schedule = bar.getProgress();    //获取进度
  27.  
      float grade = bar.getRating();    //获取星星个数,即获取等级
  28.  
      float stepsize = bar.getStepSize(); //获取每一步的步长
  29.  
      int allstar = bar.getNumStars();
  30.  
      tv.setText("获取进度:" schedule "\n"
  31.  
        "获取选中星星个数:" grade "\n"
  32.  
        "每次改变的星星个数:" stepsize "\n"
  33.  
        "总星级为:" allstar);//打印文本
  34.  
     
  35.  
      }
  36.  
     
  37.  
     }
学新通

实验四

布局文件:进度条

  1.  
     <?xml version="1.0" encoding="utf-8"?>
  2.  
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
         android:layout_width="match_parent"
  4.  
         android:layout_height="match_parent"
  5.  
         android:orientation="vertical" >
  6.  
     
  7.  
         <TextView
  8.  
             android:id="@ id/textView1"
  9.  
             android:layout_width="wrap_content"
  10.  
             android:layout_height="wrap_content"
  11.  
             android:text="当前值:50"
  12.  
             android:textSize="28sp"/>
  13.  
         
  14.  
         <SeekBar
  15.  
             android:id="@ id/seekBar1"
  16.  
             android:layout_width="match_parent"
  17.  
             android:layout_height="wrap_content"
  18.  
             android:max="100"
  19.  
             android:progress="10"/>
  20.  
     
  21.  
     </LinearLayout>
学新通

源文件:

  1.  
     package com.example.seekbar4_3;
  2.  
     
  3.  
     import android.os.Bundle;
  4.  
     import android.app.Activity;
  5.  
     import android.view.Menu;
  6.  
     import android.widget.SeekBar;
  7.  
     import android.widget.SeekBar.OnSeekBarChangeListener;
  8.  
     import android.widget.TextView;
  9.  
     import android.widget.Toast;
  10.  
     
  11.  
     public class MainActivity extends Activity {
  12.  
     
  13.  
      SeekBar bar;
  14.  
      TextView tv;
  15.  
      protected void onCreate(Bundle savedInstanceState) {
  16.  
      super.onCreate(savedInstanceState);
  17.  
      setContentView(R.layout.seekbar);
  18.  
      bar = (SeekBar)findViewById(R.id.seekBar1);
  19.  
      tv = (TextView)findViewById(R.id.textView1);
  20.  
      bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  21.  
     
  22.  
      public void onStopTrackingTouch(SeekBar bar) {
  23.  
      // 结束拖动
  24.  
      Toast.makeText(MainActivity.this, "结束滑动", 10000).show();
  25.  
      }
  26.  
     
  27.  
      public void onStartTrackingTouch(SeekBar bar) {
  28.  
      // 开始拖动
  29.  
      Toast.makeText(MainActivity.this, "开始滑动", 10000).show();
  30.  
      }
  31.  
     
  32.  
      public void onProgressChanged(SeekBar bar, int progress, boolean arg2) {
  33.  
      // 拖动中
  34.  
      tv.setText("当前值:" progress);
  35.  
     
  36.  
      }
  37.  
      });
  38.  
      }
  39.  
     
  40.  
     }
学新通

实验五

布局文件1:one.xml

  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
         android:layout_width="match_parent"
  4.  
         android:layout_height="match_parent"
  5.  
         android:orientation="vertical"
  6.  
         android:background="@drawable/zzp123" >
  7.  
     
  8.  
         <TextView
  9.  
             android:layout_width="wrap_content"
  10.  
             android:layout_height="wrap_content"
  11.  
             android:text="用户名:"
  12.  
             android:textSize="26sp" />
  13.  
         
  14.  
         <EditText
  15.  
             android:id="@ id/editText1"
  16.  
             android:layout_width="match_parent"
  17.  
             android:layout_height="wrap_content"
  18.  
             android:ems="10"
  19.  
             android:hint="请输入用户姓名"
  20.  
             android:inputType="textPersonName"
  21.  
             android:textSize="26sp" >
  22.  
         
  23.  
             <requestFocus />
  24.  
         </EditText>
  25.  
         
  26.  
         <TextView
  27.  
             android:layout_width="wrap_content"
  28.  
             android:layout_height="wrap_content"
  29.  
             android:text="密码:"
  30.  
             android:textSize="26sp" />
  31.  
         
  32.  
         <EditText
  33.  
             android:id="@ id/editText_passWd"
  34.  
             android:layout_width="match_parent"
  35.  
             android:layout_height="wrap_content"
  36.  
             android:ems="10"
  37.  
             android:hint="请输入用户密码"
  38.  
             android:inputType="textPersonName"
  39.  
             android:textSize="26sp" >
  40.  
         
  41.  
             <requestFocus />
  42.  
         </EditText>
  43.  
         
  44.  
         <TextView
  45.  
             android:layout_width="wrap_content"
  46.  
             android:layout_height="50dp" />
  47.  
         
  48.  
         <Button
  49.  
             android:id="@ id/btn_jump"
  50.  
             android:layout_width="100dp"
  51.  
             android:layout_height="50dp"
  52.  
             android:layout_gravity="center"
  53.  
             android:text="登 陆"
  54.  
             android:textSize="25sp" />
  55.  
     
  56.  
     </LinearLayout>
学新通

布局文件2:two.xml

  1.  
     <?xml version="1.0" encoding="utf-8"?>
  2.  
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
         android:layout_width="match_parent"
  4.  
         android:layout_height="match_parent"
  5.  
         android:orientation="vertical"
  6.  
         android:background="@drawable/zzp123" >
  7.  
     
  8.  
         <TextView
  9.  
             
  10.  
             android:layout_width="wrap_content"
  11.  
             android:layout_height="wrap_content"
  12.  
             android:text="@string/second"
  13.  
             android:textSize="20sp" />
  14.  
         
  15.  
         <TextView
  16.  
             android:layout_width="wrap_content"
  17.  
             android:layout_height="50dp" />
  18.  
         
  19.  
         <TextView
  20.  
             android:id="@ id/txt_xianshi"
  21.  
             android:layout_width="wrap_content"
  22.  
             android:layout_height="wrap_content"
  23.  
             android:layout_gravity="center"
  24.  
             android:text="此处显示传值结果"
  25.  
             android:textSize="20sp" />
  26.  
     
  27.  
     </LinearLayout>
学新通

源文件1:MainActivity.java

  1.  
    package com.example.zhangzhipeng5_1;
  2.  
     
  3.  
     import android.os.Bundle;
  4.  
     import android.app.Activity;
  5.  
     import android.content.Intent;
  6.  
     import android.view.Menu;
  7.  
     import android.view.View;
  8.  
     import android.view.View.OnClickListener;
  9.  
     import android.widget.Button;
  10.  
     import android.widget.EditText;
  11.  
     
  12.  
     public class MainActivity extends Activity {
  13.  
      Button btn;
  14.  
      EditText edname;
  15.  
      @Override
  16.  
      protected void onCreate(Bundle savedInstanceState) {
  17.  
      super.onCreate(savedInstanceState);
  18.  
      setContentView(R.layout.one);
  19.  
      // 初始化控件
  20.  
      btn=(Button) findViewById(R.id.btn_jump);
  21.  
      edname = (EditText)findViewById(R.id.editText1);
  22.  
      btn.setOnClickListener(lis);
  23.  
      }
  24.  
      OnClickListener lis = new OnClickListener() {
  25.  
      public void onClick(View arg0) {
  26.  
     
  27.  
     
  28.  
      String strname = edname.getText().toString();
  29.  
      //打包
  30.  
      Bundle bundle = new Bundle();
  31.  
      bundle.putString("name", strname);//键值对
  32.  
     
  33.  
      /*Intent intent = new Intent();
  34.  
      intent.setClass(MainActivity.this, SecondActivity.class);*/
  35.  
      //与上方语句等价
  36.  
      Intent intent = new Intent(MainActivity.this, SecondActivity.class);
  37.  
      intent.putExtra("name", strname);//类似于物流公司装车
  38.  
      //启动
  39.  
      startActivity(intent);
  40.  
     
  41.  
      }
  42.  
      };
  43.  
     
  44.  
     }
学新通

源文件2:SecondActivity.java

  1.  
     package com.example.zhangzhipeng5_1;
  2.  
     
  3.  
     import android.app.Activity;
  4.  
     import android.content.Intent;
  5.  
     import android.os.Bundle;
  6.  
     import android.widget.TextView;
  7.  
     
  8.  
     public class SecondActivity extends Activity {
  9.  
     
  10.  
     
  11.  
     
  12.  
      TextView tv_xianshi;
  13.  
      @Override
  14.  
      protected void onCreate(Bundle savedInstanceState) {
  15.  
      // TODO Auto-generated method stub
  16.  
     
  17.  
      super.onCreate(savedInstanceState);
  18.  
      setContentView(R.layout.two);
  19.  
      tv_xianshi = (TextView)findViewById(R.id.txt_xianshi);
  20.  
      //类似快点得到物流公司车辆
  21.  
      Intent inte = getIntent();
  22.  
      //类似快递点取得商品编号
  23.  
      Bundle bundle = inte.getExtras();
  24.  
      //类似快递点根据编号把商品给买家
  25.  
      String nameinput = bundle.getString("name");
  26.  
      tv_xianshi.setText("第一个页面输入的用户名是:" nameinput);
  27.  
     
  28.  
      }
  29.  
     }
学新通

实验六 (一) 数据库SQLite

布局文件:(不用新建,直接使用自带的actibity.xml文件)

  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
         android:layout_width="match_parent"
  4.  
         android:layout_height="match_parent"
  5.  
         android:orientation="vertical" >
  6.  
     
  7.  
         <EditText
  8.  
             android:id="@ id/ed_name"
  9.  
             android:layout_width="match_parent"
  10.  
             android:layout_height="wrap_content"
  11.  
             android:hint="@string/ed_tishi" />
  12.  
         
  13.  
         <Button
  14.  
             android:id="@ id/btn_write"
  15.  
             android:layout_width="match_parent"
  16.  
             android:layout_height="wrap_content"
  17.  
             android:text="@string/btn_writestr"
  18.  
             android:textSize="20sp"
  19.  
             android:onClick="onClick_btn_wtite"/>
  20.  
         
  21.  
         <Button
  22.  
             android:id="@ id/btn_read"
  23.  
             android:layout_width="match_parent"
  24.  
             android:layout_height="wrap_content"
  25.  
             android:text="@string/btn_readstr"
  26.  
             android:textSize="20sp"
  27.  
             android:onClick="onClick_btn_read"
  28.  
            />
  29.  
         
  30.  
         <TextView
  31.  
             android:id="@ id/txt_display"
  32.  
             android:layout_width="wrap_content"
  33.  
             android:layout_height="wrap_content"
  34.  
             android:layout_gravity="center"
  35.  
             android:text="@string/txt_display"
  36.  
             android:textSize="20sp" />
  37.  
     
  38.  
     </LinearLayout>
学新通

源文件:

  1.  
    package com.example.sharepredemo6_1;
  2.  
     
  3.  
     import android.os.Bundle;
  4.  
     import android.app.Activity;
  5.  
     import android.content.SharedPreferences;
  6.  
     import android.content.SharedPreferences.Editor;
  7.  
     import android.view.Menu;
  8.  
     import android.view.View;
  9.  
     import android.widget.Button;
  10.  
     import android.widget.EditText;
  11.  
     import android.widget.TextView;
  12.  
     import android.widget.Toast;
  13.  
     
  14.  
     public class MainActivity extends Activity {
  15.  
     
  16.  
      Button btn_read,btn_write;
  17.  
      EditText ed_name;
  18.  
      TextView txt_display;
  19.  
     
  20.  
      //自定义方法   find(),用来初始化控件
  21.  
      private void find(){
  22.  
      btn_read = (Button)findViewById(R.id.btn_read);
  23.  
      btn_write = (Button)findViewById(R.id.btn_write);
  24.  
      ed_name = (EditText)findViewById(R.id.ed_name);
  25.  
      txt_display = (TextView)findViewById(R.id.txt_display);
  26.  
      }
  27.  
      protected void onCreate(Bundle savedInstanceState) {
  28.  
      super.onCreate(savedInstanceState);
  29.  
      setContentView(R.layout.actibity_xml);
  30.  
      find();//注意:因为必须要在onCrate中初始化所有控件,所以此处调用find()方法;
  31.  
     
  32.  
      }
  33.  
      //自定义方法   onClick_btn_write(),用来实现Toast提示写入数据是否成功
  34.  
      public void onClick_btn_wtite(View v){
  35.  
     
  36.  
      String strname = ed_name.getText().toString();
  37.  
      //获取 SharedPreferences对象
  38.  
      SharedPreferences sp = getSharedPreferences("file1", MODE_APPEND);
  39.  
      //获取编辑器
  40.  
      Editor editor = sp.edit();
  41.  
      //以键值对的形式存储数据
  42.  
      editor.putString("Name", strname);
  43.  
      editor.putInt("ID", 1234567);
  44.  
      editor.putString("Number", "1906030141");
  45.  
      //提交
  46.  
      editor.commit();
  47.  
      Toast.makeText(MainActivity.this,"写入数据成功", Toast.LENGTH_LONG).show();
  48.  
      }
  49.  
     
  50.  
     public void onClick_btn_read(View v){
  51.  
     
  52.  
     
  53.  
      //获取 SharedPreferences对象
  54.  
      SharedPreferences sp = getSharedPreferences("file1", MODE_APPEND);
  55.  
      //获取编辑器
  56.  
      Editor editor = sp.edit();
  57.  
      //获取存储数据
  58.  
      String strget = sp.getString("Name", "");
  59.  
      String number = sp.getString("Number", "");
  60.  
      Integer idstr = sp.getInt("ID", 0);
  61.  
      txt_display.setText("读取数据为如下" '\n' "用户名:" strget '\n' "学号:" number '\n' "身份证:" idstr);
  62.  
      //提交
  63.  
      editor.commit();
  64.  
      Toast.makeText(MainActivity.this,"读取数据成功", Toast.LENGTH_LONG).show();
  65.  
      }
  66.  
     
  67.  
     
  68.  
     }
学新通

String.xml:

  1.  
     <?xml version="1.0" encoding="utf-8"?>
  2.  
     <resources>
  3.  
     
  4.  
         <string name="app_name">章志鹏6.1</string>
  5.  
         <string name="action_settings">Settings</string>
  6.  
         <string name="ed_tishi">imput name:</string>
  7.  
         <string name="btn_writestr">向SharedPreferences中写入数据</string>
  8.  
         <string name="btn_readstr">从SharedPreferences中读取数据</string>
  9.  
         <string name="txt_display">此处显从读取数据结果</string>
  10.  
     
  11.  
     </resources>

实验六 (二)存储

源文件1:MainActivity

  1.  
    package com.example.slitedemo6_2;
  2.  
     
  3.  
     import android.os.Bundle;
  4.  
     import android.app.Activity;
  5.  
     import android.database.sqlite.SQLiteDatabase;
  6.  
     import android.view.Menu;
  7.  
     
  8.  
     public class MainActivity extends Activity {
  9.  
     
  10.  
      @Override
  11.  
      protected void onCreate(Bundle savedInstanceState) {
  12.  
      super.onCreate(savedInstanceState);
  13.  
      setContentView(R.layout.activity_main);
  14.  
      DBHelp dbhelp = new DBHelp(MainActivity.this, "kecheng.db", null,1);
  15.  
      SQLiteDatabase db = dbhelp.getReadableDatabase();
  16.  
      db.execSQL("insert into course(c_name, type,teacherID) values('Android程序设计','选修课',13602)");
  17.  
      db.execSQL("insert into student(s_name, sex,c_name) values('张三','男','C ')");
  18.  
      db.close();
  19.  
     
  20.  
     
  21.  
      }
  22.  
     
  23.  
     
  24.  
     
  25.  
     }
学新通

源文件2:DBHelp

  1.  
    package com.example.slitedemo6_2;
  2.  
     
  3.  
     import android.content.Context;
  4.  
     import android.database.DatabaseErrorHandler;
  5.  
     import android.database.sqlite.SQLiteDatabase;
  6.  
     import android.database.sqlite.SQLiteDatabase.CursorFactory;
  7.  
     import android.database.sqlite.SQLiteOpenHelper;
  8.  
     
  9.  
     public class DBHelp extends SQLiteOpenHelper {
  10.  
     
  11.  
      // 利用构造方法创建数据库
  12.  
      public DBHelp(Context context, String name, CursorFactory factory,
  13.  
      int version) {
  14.  
      super(context, "kecheng.db", null, 1);
  15.  
     
  16.  
      }
  17.  
     
  18.  
      public void onCreate(SQLiteDatabase db) {
  19.  
      String create_table1 = "create table if not exists course("
  20.  
      "_id integer primary key autoincrement,"
  21.  
      "c_name text not null,"
  22.  
      "type text not null,"
  23.  
      "teacherID integer not null)";
  24.  
      db.execSQL(create_table1);
  25.  
      db.execSQL("insert into course(c_name, type,teacherID) values('java程序设计','必修课',13600)");
  26.  
      db.execSQL("insert into course(c_name, type,teacherID) values('Python程序设计','必修课',13601)");
  27.  
     
  28.  
     
  29.  
      String create_table2 = "create table if not exists student("
  30.  
      "_id integer primary key autoincrement,"
  31.  
      "s_name text not null,"
  32.  
      "sex text not null,"
  33.  
      "c_name text not null)";
  34.  
      db.execSQL(create_table2);
  35.  
      db.execSQL("insert into student(s_name, sex,c_name) values('章志鹏','男','java程序设计')");
  36.  
      db.execSQL("insert into student(s_name, sex,c_name) values('徐轶涵','男','C语言程序设计')");
  37.  
      }
  38.  
     
  39.  
      @Override
  40.  
      public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
  41.  
      // TODO Auto-generated method stub
  42.  
     
  43.  
      }
  44.  
     
  45.  
     }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfafkc
系列文章
更多 icon
同类精品
更多 icon
继续加载