Android 布局 实验4
来源:互联网 发布:莫言丑化中国知乎 编辑:程序博客网 时间:2024/06/05 20:24
Android 布局 实验4
Android 布局 实验4
一、建立Android项目
建立项目前几篇文章已经说过了这里不再累述。
二、编写资源文件(字符文件)
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, LayoutActivity!</string> <string name="app_name">何小虎的应用_2</string> <string name="notice">请输入:</string> <string name="sure">确定</string> <string name="cancle">取消</string> <string name="warning">输入内容为空!</string></resources>
三、编写资源文件(布局文件)
main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10px" ><TextView android:id="@+id/notice" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/notice"/> <EditText android:id="@+id/content" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_below="@id/notice" /> <Button android:id="@+id/submit" android:text="@string/sure" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/content" android:layout_alignParentRight="true" android:layout_marginLeft="10px" /> <Button android:id="@+id/cancle" android:text="@string/cancle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/content" android:layout_alignTop="@id/submit" android:layout_toLeftOf="@id/submit" /> </RelativeLayout>
showcontent.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10px" ><TextView android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="wrap_content" /></RelativeLayout>
四、编写核心程序(屏幕)
LayoutActivity.java
package com.home.activity;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;/** * 提交前台文本框中的数据,提交给另一个Activity处理. * * @author Administrator * */public class LayoutActivity extends Activity{private Button OKButton = null;private Button CancleButton = null;private EditText content = null;private String con = null;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);// 获得表单数据OKButton = (Button) this.findViewById(R.id.submit);CancleButton = (Button) this.findViewById(R.id.cancle);content = (EditText) this.findViewById(R.id.content);// 给确定按钮添加点击事件OKButton.setOnClickListener(new OKListener());// 取消按钮监听事件CancleButton.setOnClickListener(new CancleListener());}// 确定按钮监听器private class OKListener implements OnClickListener{@Overridepublic void onClick(View v){// 取得表单输入框内容(不要犯错: 不能写在onCreate()中,这是还没输入内容呢)con = content.getText().toString();if (null != con && !con.equals("")){Intent intent = new Intent();intent.setClass(LayoutActivity.this,ContentCollectionActivity.class);intent.putExtra("content", con);startActivity(intent);}else{Toast.makeText(LayoutActivity.this, R.string.warning,Toast.LENGTH_LONG).show();}}}// 取消按钮监听器private class CancleListener implements OnClickListener{@Overridepublic void onClick(View v){Toast.makeText(LayoutActivity.this, "系统退出!",Toast.LENGTH_LONG).show();finish();}}}
ContentCollectionActivity
package com.home.activity;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class ContentCollectionActivity extends Activity{private Intent intent = null;private String content = "";private TextView text = null;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.showcontent);text = (TextView)this.findViewById(R.id.content);intent = this.getIntent();content = intent.getStringExtra("content").toString();text.setText("传过来的内容是:" + content);}}
五、编写清单文件
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.home.activity" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/hexiaohu" android:label="@string/app_name"> <activity android:name=".LayoutActivity" 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=".ContentCollectionActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
六、运行程序
如何将项目安装在模拟器中之前文章说过了,这里不做累述。
- Android 布局 实验4
- Android 布局 实验1
- Android 布局 实验2
- Android 布局 实验3
- Android:线性布局和相对布局的效率对比小实验
- Android布局(4)--相对布局(RelativeLayout)
- Android---4---布局之RelativeLayout
- Android 开发系列 4 布局
- [android]笔记4-线性布局
- Android 布局:相对布局
- Android(4)---Android 控件布局常用属性
- android 布局
- Android布局
- android布局
- Android 布局
- android 布局
- Android 布局
- Android布局
- [Other]计算机组成原理分解实验:实验四 时序系统
- SVN相关配置
- 时间序列作业
- MySQL数据库优化总结
- 银行业务调度系统
- Android 布局 实验4
- vim常用命令
- eclipse快捷键
- 在Ubuntu下打开pdf时中文乱码解决
- 向函数中传入二维数组并返回它的具体实现
- 控件移动
- Hetergeneous Parallel Programming编程作业之MP0: Initial Lab Tour with Device Query
- 时间序列作业
- 网络编程