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> 


 

 

 

六、运行程序

如何将项目安装在模拟器中之前文章说过了,这里不做累述。