Android开发实验参考代码

实验三

布局文件:星级评分条

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical" >
 ​
     <RatingBar
         android:id="@+id/ratingBar1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:isIndicator="false"
         android:numStars="5"
         android:rating="3"
         android:stepSize="0.5" />
     
     <Button
         android:id="@+id/button1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="提交" 
         android:onClick="tijiao"/>
     
     <TextView
         android:layout_width="wrap_content"
         android:layout_height="50dp"
         android:text="" />
     
     <TextView
         android:id="@+id/textView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:singleLine="false"
         android:text="此处显示星级评分条信息"
         android:textSize="20sp" />
 ​
 </LinearLayout>

源文件:

 
package com.example.zhangzhipeng3_3;
 ​
 import android.os.Bundle;
 import android.app.Activity;
 import android.view.Menu;
 import android.view.View;
 import android.widget.RatingBar;
 import android.widget.TextView;
 ​
 public class MainActivity extends Activity {
     
 ​
     RatingBar bar; 
     TextView tv;
     int alltar;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.ratingbar);
     }
     
     public void tijiao(View v){
         bar = (RatingBar) findViewById(R.id.ratingBar1);
         tv = (TextView)findViewById(R.id.textView1);
         
         int schedule = bar.getProgress();    //获取进度
         float grade = bar.getRating();    //获取星星个数,即获取等级
         float stepsize = bar.getStepSize(); //获取每一步的步长
         int allstar = bar.getNumStars();
         tv.setText("获取进度:"+schedule+"\n"+
                    "获取选中星星个数:"+grade+"\n"+
                    "每次改变的星星个数:"+stepsize+"\n"+
                    "总星级为:"+allstar);//打印文本
         
     }
 ​
 }

实验四

布局文件:进度条

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical" >
 ​
     <TextView
         android:id="@+id/textView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="当前值:50"
         android:textSize="28sp"/>
     
     <SeekBar
         android:id="@+id/seekBar1"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:max="100"
         android:progress="10"/>
 ​
 </LinearLayout>

源文件:

 package com.example.seekbar4_3;
 ​
 import android.os.Bundle;
 import android.app.Activity;
 import android.view.Menu;
 import android.widget.SeekBar;
 import android.widget.SeekBar.OnSeekBarChangeListener;
 import android.widget.TextView;
 import android.widget.Toast;
 ​
 public class MainActivity extends Activity {
 ​
     SeekBar bar;
     TextView tv;
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.seekbar);
         bar = (SeekBar)findViewById(R.id.seekBar1);
         tv = (TextView)findViewById(R.id.textView1);
         bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
             
             public void onStopTrackingTouch(SeekBar bar) {
                 // 结束拖动
                 Toast.makeText(MainActivity.this, "结束滑动", 10000).show();
             }
             
             public void onStartTrackingTouch(SeekBar bar) {
                 // 开始拖动
                 Toast.makeText(MainActivity.this, "开始滑动", 10000).show();
             }
     
             public void onProgressChanged(SeekBar bar, int progress, boolean arg2) {
                 // 拖动中
                 tv.setText("当前值:"+progress);
                 
             }
         });
     }
 ​
 }

实验五

布局文件1:one.xml

 
<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     android:background="@drawable/zzp123" >
 ​
     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="用户名:"
         android:textSize="26sp" />
     
     <EditText
         android:id="@+id/editText1"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:ems="10"
         android:hint="请输入用户姓名"
         android:inputType="textPersonName"
         android:textSize="26sp" >
     
         <requestFocus />
     </EditText>
     
     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="密码:"
         android:textSize="26sp" />
     
     <EditText
         android:id="@+id/editText_passWd"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:ems="10"
         android:hint="请输入用户密码"
         android:inputType="textPersonName"
         android:textSize="26sp" >
     
         <requestFocus />
     </EditText>
     
     <TextView
         android:layout_width="wrap_content"
         android:layout_height="50dp" />
     
     <Button
         android:id="@+id/btn_jump"
         android:layout_width="100dp"
         android:layout_height="50dp"
         android:layout_gravity="center"
         android:text="登  陆"
         android:textSize="25sp" />
 ​
 </LinearLayout>

布局文件2:two.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     android:background="@drawable/zzp123" >
 ​
     <TextView
         
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/second"
         android:textSize="20sp" />
     
     <TextView
         android:layout_width="wrap_content"
         android:layout_height="50dp" />
     
     <TextView
         android:id="@+id/txt_xianshi"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:text="此处显示传值结果"
         android:textSize="20sp" />
 ​
 </LinearLayout>

源文件1:MainActivity.java

 
package com.example.zhangzhipeng5_1;
 ​
 import android.os.Bundle;
 import android.app.Activity;
 import android.content.Intent;
 import android.view.Menu;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.EditText;
 ​
 public class MainActivity extends Activity {
     Button btn;
     EditText edname;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.one);
         // 初始化控件
         btn=(Button) findViewById(R.id.btn_jump);
         edname = (EditText)findViewById(R.id.editText1);
         btn.setOnClickListener(lis);
     }
     OnClickListener lis = new OnClickListener() {
         public void onClick(View arg0) {
             
 ​
             String strname = edname.getText().toString();
             //打包
             Bundle bundle = new Bundle();
             bundle.putString("name", strname);//键值对
             
             /*Intent intent = new Intent();
             intent.setClass(MainActivity.this, SecondActivity.class);*/
             //与上方语句等价
             Intent intent = new Intent(MainActivity.this, SecondActivity.class);
             intent.putExtra("name", strname);//类似于物流公司装车
             //启动
             startActivity(intent);
             
         }
     };
 ​
 }

源文件2:SecondActivity.java

 package com.example.zhangzhipeng5_1;
 ​
 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.widget.TextView;
 ​
 public class SecondActivity extends Activity {
     
     
 ​
     TextView tv_xianshi;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         // TODO Auto-generated method stub
         
         super.onCreate(savedInstanceState);
         setContentView(R.layout.two);
         tv_xianshi = (TextView)findViewById(R.id.txt_xianshi);
         //类似快点得到物流公司车辆
         Intent inte = getIntent();
         //类似快递点取得商品编号
         Bundle bundle = inte.getExtras();
         //类似快递点根据编号把商品给买家
         String nameinput = bundle.getString("name");
         tv_xianshi.setText("第一个页面输入的用户名是:" + nameinput);        
 ​
     }
 }

实验六 (一) 数据库SQLite

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

 
<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical" >
 ​
     <EditText
         android:id="@+id/ed_name"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:hint="@string/ed_tishi" />
     
     <Button
         android:id="@+id/btn_write"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="@string/btn_writestr"
         android:textSize="20sp" 
         android:onClick="onClick_btn_wtite"/>
     
     <Button
         android:id="@+id/btn_read"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="@string/btn_readstr"
         android:textSize="20sp" 
         android:onClick="onClick_btn_read"
        />
     
     <TextView
         android:id="@+id/txt_display"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:text="@string/txt_display"
         android:textSize="20sp" />
 ​
 </LinearLayout>

源文件:

 
package com.example.sharepredemo6_1;
 ​
 import android.os.Bundle;
 import android.app.Activity;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.view.Menu;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.TextView;
 import android.widget.Toast;
 ​
 public class MainActivity extends Activity {
 ​
     Button btn_read,btn_write;
     EditText ed_name;
     TextView txt_display;
     
     //自定义方法   find(),用来初始化控件
     private void find(){
         btn_read = (Button)findViewById(R.id.btn_read);
         btn_write = (Button)findViewById(R.id.btn_write);
         ed_name = (EditText)findViewById(R.id.ed_name);
         txt_display = (TextView)findViewById(R.id.txt_display);
     }
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.actibity_xml);
         find();//注意:因为必须要在onCrate中初始化所有控件,所以此处调用find()方法;
     
     }
     //自定义方法   onClick_btn_write(),用来实现Toast提示写入数据是否成功
     public void onClick_btn_wtite(View v){
         
         String strname = ed_name.getText().toString();
         //获取 SharedPreferences对象
         SharedPreferences sp = getSharedPreferences("file1", MODE_APPEND);
         //获取编辑器
         Editor editor = sp.edit();
         //以键值对的形式存储数据
         editor.putString("Name", strname);
         editor.putInt("ID", 1234567);
         editor.putString("Number", "1906030141");
         //提交
         editor.commit();
         Toast.makeText(MainActivity.this,"写入数据成功", Toast.LENGTH_LONG).show();
     }
 ​
 public void onClick_btn_read(View v){
         
 ​
         //获取 SharedPreferences对象
         SharedPreferences sp = getSharedPreferences("file1", MODE_APPEND);
         //获取编辑器
         Editor editor = sp.edit();
         //获取存储数据
         String strget = sp.getString("Name", "");
         String number = sp.getString("Number", "");
         Integer idstr = sp.getInt("ID", 0);
         txt_display.setText("读取数据为如下"+'\n'+"用户名:"+strget+'\n'+"学号:"+number+'\n'+"身份证:"+idstr);
         //提交
         editor.commit();
         Toast.makeText(MainActivity.this,"读取数据成功", Toast.LENGTH_LONG).show();
     }
 ​
 ​
 }

String.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
 ​
     <string name="app_name">章志鹏6.1</string>
     <string name="action_settings">Settings</string>
     <string name="ed_tishi">imput name:</string>
     <string name="btn_writestr">向SharedPreferences中写入数据</string>
     <string name="btn_readstr">从SharedPreferences中读取数据</string>
     <string name="txt_display">此处显从读取数据结果</string>
 ​
 </resources>

实验六 (二)存储

源文件1:MainActivity

 
package com.example.slitedemo6_2;
 ​
 import android.os.Bundle;
 import android.app.Activity;
 import android.database.sqlite.SQLiteDatabase;
 import android.view.Menu;
 ​
 public class MainActivity extends Activity {
 ​
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         DBHelp dbhelp = new DBHelp(MainActivity.this, "kecheng.db", null,1);
         SQLiteDatabase db = dbhelp.getReadableDatabase();
         db.execSQL("insert into course(c_name, type,teacherID) values('Android程序设计','选修课',13602)");
         db.execSQL("insert into student(s_name, sex,c_name) values('张三','男','C++')");
         db.close();
 ​
 ​
     }
 ​
 ​
 ​
 }

源文件2:DBHelp

 
package com.example.slitedemo6_2;
 ​
 import android.content.Context;
 import android.database.DatabaseErrorHandler;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteDatabase.CursorFactory;
 import android.database.sqlite.SQLiteOpenHelper;
 ​
 public class DBHelp extends SQLiteOpenHelper {
 ​
     // 利用构造方法创建数据库
     public DBHelp(Context context, String name, CursorFactory factory,
             int version) {
         super(context, "kecheng.db", null, 1);
     
     }
     
     public void onCreate(SQLiteDatabase db) {
         String create_table1 = "create table if not exists course(" +
                 "_id integer primary key autoincrement," +
                 "c_name text not null," +
                 "type text not null," +
                 "teacherID integer not null)";
         db.execSQL(create_table1);
         db.execSQL("insert into course(c_name, type,teacherID) values('java程序设计','必修课',13600)");
         db.execSQL("insert into course(c_name, type,teacherID) values('Python程序设计','必修课',13601)");
     
 ​
         String create_table2 = "create table if not exists student(" +
                 "_id integer primary key autoincrement," +
                 "s_name text not null," +
                 "sex text not null," +
                 "c_name text not null)";
         db.execSQL(create_table2);
         db.execSQL("insert into student(s_name, sex,c_name) values('章志鹏','男','java程序设计')");
         db.execSQL("insert into student(s_name, sex,c_name) values('徐轶涵','男','C语言程序设计')");
     }
     
     @Override
     public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
         // TODO Auto-generated method stub
     
     }
 ​
 }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值