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;
-
-
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;
-
-
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;
-
-
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 {
-
-
-
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语言程序设计')");
-
}
-
-
-
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
-
// TODO Auto-generated method stub
-
-
}
-
-
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgfafkc
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01