Android 基础UI编程2
Android 基础UI编程
标题、状态栏的隐藏
标题栏隐藏
在Activity.setCurrentView();之前调用此方法
private void HideTitle() {// TODO Auto-generated method stubrequestWindowFeature(Window.FEATURE_NO_TITLE);}
private void HideStatusBar() {// TODO Auto-generated method stub//隐藏标题requestWindowFeature(Window.FEATURE_NO_TITLE);//定义全屏参数int flag=WindowManager.LayoutParams.FLAG_FULLSCREEN;//获得窗口对象Window myWindow=this.getWindow();//设置Flag标识myWindow.setFlags(flag,flag);}
<?xml version="1.0" encoding="utf-8"?><resources><style name="myStyle_Text1"><item name="android:textSize">25sp</item><item name="android:textColor">#80FF00</item></style><style name="myStyle_Text2"><item name="android:textSize">18sp</item><item name="android:textColor">#0C688E</item><item name="android:fromAlpha">0.0</item><item name="android:toAlpha" >0.0</item></style></resources>
<?xml version="1.0" encoding="utf-8"?><resources><string name="string_A">应用myStyle_Text1</string><string name="string_B">应用myStyle_Text2</string></resources>
<TextViewandroid:id="@+id/TextView01"android:layout_height="wrap_content"android:layout_width="fill_parent"android:gravity="center_vertical|center_horizontal"android:text="@string/string_A"></TextView><TextViewandroid:id="@+id/TextView02"android:layout_height="wrap_content"android:layout_width="fill_parent"android:gravity="center_vertical|center_horizontal"android:text="@string/string_B"></TextView>
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:id="@+id/TextView01"style="@style/myStyle_Text1"android:layout_height="wrap_content"android:layout_width="fill_parent"android:gravity="center_vertical|center_horizontal"android:text="@string/string_A"></TextView><TextViewandroid:id="@+id/TextView02"style="@style/myStyle_Text2"android:layout_height="wrap_content"android:layout_width="fill_parent"android:gravity="center_vertical|center_horizontal"android:text="@string/string_B"></TextView></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:id="@+id/show_TextView"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"/><Buttonandroid:id="@+id/Click_Button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="点击"/></LinearLayout>
show= (TextView)findViewById(R.id.show_TextView);press=(Button)findViewById(R.id.Click_Button);
press.setOnClickListener(new Button.OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stub}});
press.setOnClickListener(new Button.OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubshow.setText("Hi , Google Android!");}});
<?xml version="1.0" encoding="utf-8"?><resources><string name="layout1">this is Layout 1</string><string name="layout2">This is Layout 2</string><string name="app_name">Ex8_UI</string></resources>
<?xml version="1.0" encoding="utf-8"?><resources><color name="black">#000000</color><color name="white">#FFFFFFFF</color></resources>
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@color/black"xmlns:android="http://schemas.android.com/apk/res/android"> <TextViewandroid:id="@+id/text1"android:textSize="24sp"android:layout_width="186px"android:layout_height="29px"android:layout_x="70px"android:layout_y="32px"android:text="@string/layout1"></TextView><Buttonandroid:id="@+id/button1"android:layout_width="118px"android:layout_height="wrap_content"android:layout_x="100px"android:layout_y="82px"android:text="Go to Layout2"></Button></AbsoluteLayout>
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@color/white"xmlns:android="http://schemas.android.com/apk/res/android"> <TextViewandroid:id="@+id/text2"android:textSize="24sp"android:layout_width="186px"android:layout_height="29px"android:layout_x="70px"android:layout_y="32px"android:textColor="@color/black"android:text="@string/layout2"> </TextView><Buttonandroid:id="@+id/button2"android:layout_width="118px"android:layout_height="wrap_content"android:layout_x="100px"android:layout_y="82px"android:text="Go to Layout1"></Button></AbsoluteLayout>
package zyf.Ex8_UI;import android.app.Activity;/* import 相关class */import android.os.Bundle;import android.view.View;import android.widget.Button;public class Ex8_UI extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);/* 载入main.xml Layout */setContentView(R.layout.main);// 默认启动布局/* 以findViewById()取得Button 对象,并添加onClickListener */Button b1 = (Button) findViewById(R.id.button1);b1.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {jumpToLayout2();// 调用跳转方法jumpToLayout2()}});} /* method jumpToLayout2:将layout 由main.xml 切换成mylayout.xml */public void jumpToLayout2() {/* 将layout 改成mylayout.xml */setContentView(R.layout.mylayout);/* 以findViewById()取得Button 对象,并添加onClickListener */Button b2 = (Button) findViewById(R.id.button2);b2.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {jumpToLayout1();// 调用跳转方法jumpToLayout1()}});} /* method jumpToLayout1:将layout 由mylayout.xml 切换成main.xml */public void jumpToLayout1() {/* 将layout 改成main.xml */setContentView(R.layout.main);/* 以findViewById()取得Button 对象,并添加onClickListener */Button b1 = (Button) findViewById(R.id.button1);b1.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {jumpToLayout2();// 调用跳转方法jumpToLayout2()}});}}
<?xml version="1.0" encoding="utf-8"?><resources><string name="hello">Hello World, Ex9_UI!</string><string name="app_name">Ex9_UI</string><string name="act1">This is Activity 1!</string><string name="act2">This is Activity 2!</string></resources>
<?xml version="1.0" encoding="utf-8"?><resources><color name="black">#000000</color><color name="white">#FFFFFFFF</color></resources>
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@color/black"xmlns:android="http://schemas.android.com/apk/res/android"> <TextViewandroid:id="@+id/text1"android:textSize="24sp"android:layout_width="186px"android:layout_height="29px"android:layout_x="70px"android:layout_y="32px"android:text="@string/act1"></TextView><Buttonandroid:id="@+id/button1"android:layout_width="118px"android:layout_height="wrap_content"android:layout_x="100px"android:layout_y="82px"android:text="Go to Activity2"></Button></AbsoluteLayout>
<AbsoluteLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@color/white"xmlns:android="http://schemas.android.com/apk/res/android"> <TextViewandroid:id="@+id/text2"android:textSize="24sp"android:layout_width="186px"android:layout_height="29px"android:layout_x="70px"android:layout_y="32px"android:textColor="@color/black"android:text="@string/act2"></TextView><Buttonandroid:id="@+id/button2"android:layout_width="118px"android:layout_height="wrap_content"android:layout_x="100px"android:layout_y="82px"android:text="Go to Activity1"></Button></AbsoluteLayout>
package zyf.Ex9_UI;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class SecondActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);/* 载入mylayout.xml Layout */setContentView(R.layout.mylayout);/* 以findViewById()取得Button 对象,并添加onClickListener */Button b2 = (Button) findViewById(R.id.button2);b2.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {/* new 一个Intent 对象,并指定要启动的class */Intent intent = new Intent();intent.setClass(SecondActivity.this, Ex9_UI.class);/* 调用一个新的Activity */startActivity(intent);/* 关闭原本的Activity */SecondActivity.this.finish();}});}}
package zyf.Ex9_UI;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class Ex9_UI extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);/* 载入main.xml Layout */setContentView(R.layout.main);/* 以findViewById()取得Button 对象,并添加onClickListener */Button b1 = (Button) findViewById(R.id.button1);b1.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {/* new 一个Intent 对象,并指定要启动的class */Intent intent = new Intent();intent.setClass(Ex9_UI.this, SecondActivity.class);/* 调用一个新的Activity */startActivity(intent);/* 关闭原本的Activity */Ex9_UI.this.finish();}});}}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="zyf.Ex9_UI"android:versionCode="1"android:versionName="1.0"><application android:icon="@drawable/icon"android:label="@string/app_name" ><activity android:name=".Ex9_UI"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="SecondActivity"></activity></application><uses-sdk android:minSdkVersion="2" /></manifest>
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayoutandroid:id="@+id/widget0"android:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android" ><TextViewandroid:id="@+id/showText"android:layout_width="wrap_content"android:layout_height="26px"android:text="计算你的标准体重!"android:textSize="25px"android:layout_x="65px"android:layout_y="21px"></TextView><TextViewandroid:id="@+id/text_Sex"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="性别:"android:layout_x="71px"android:layout_y="103px"></TextView><TextViewandroid:id="@+id/text_Height"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="身高:"android:layout_x="72px"android:layout_y="169px"></TextView><RadioGroupandroid:id="@+id/radioGroup"android:layout_width="wrap_content"android:layout_height="37px"android:orientation="horizontal"android:layout_x="124px"android:layout_y="101px"><RadioButtonandroid:id="@+id/Sex_Man"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男"></RadioButton><RadioButtonandroid:id="@+id/Sex_Woman"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女"></RadioButton></RadioGroup><EditTextandroid:id="@+id/height_Edit"android:layout_width="123px"android:layout_height="wrap_content"android:text=""android:textSize="18sp"android:layout_x="124px"android:layout_y="160px"></EditText><Buttonandroid:id="@+id/button_OK"android:layout_width="80px"android:layout_height="wrap_content"android:text="计算"android:layout_x="125px"android:layout_y="263px"></Button></AbsoluteLayout>
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"> <TextViewandroid:id="@+id/text1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:layout_x="50px"android:layout_y="72px"></TextView></AbsoluteLayout>
package zyf.Ex10_UI;import android.app.Activity;import android.os.Bundle;public class BMIActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="zyf.Ex10_UI"android:versionCode="1"android:versionName="1.0"><application android:icon="@drawable/icon"android:label="@string/app_name" ><activity android:name=".Ex10_UI"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="BMIActivity"></activity></application><uses-sdk android:minSdkVersion="2" /></manifest>
package zyf.Ex10_UI;/* import 相关class */import java.text.DecimalFormat;import java.text.NumberFormat;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class BMIActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);/* 加载main.xml Layout */setContentView(R.layout.mylayout);/* 取得Intent 中的Bundle 对象*/Bundle bunde = this.getIntent().getExtras();/* 取得Bundle 对象中的数据*/String sex = bunde.getString("sex");double height = bunde.getDouble("height");/* 判断性别*/String sexText = "";if (sex.equals("M")) {sexText = "男性";} else {sexText = "女性";} /* 取得标准体重*/String weight = this.getWeight(sex, height);/* 设置输出文字*/TextView tv1 = (TextView) findViewById(R.id.text1);tv1.setText("你是一位" + sexText + "\n你的身高是" + height +"厘米\n你的标准体重是"+ weight + "公斤");}/* 四舍五入的method */private String format(double num) {NumberFormat formatter = new DecimalFormat("0.00");String s = formatter.format(num);return s;}/* 以findViewById()取得Button 对象,并添加onClickListener */private String getWeight(String sex, double height) {String weight = "";if (sex.equals("M")) {weight = format((height - 80) * 0.7);} else {weight = format((height - 70) * 0.6);} return weight;}}
package zyf.Ex10_UI;/* import 相关class */import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.RadioButton;public class Ex10_UI extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);/* 载入main.xml Layout */setContentView(R.layout.main);/* 以findViewById()取得Button 对象,并添加onClickListener */Button ok = (Button) findViewById(R.id.button_OK);ok.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {/* 取得输入的身高*/EditText et = (EditText) findViewById(R.id.height_Edit);double height = Double.parseDouble(et.getText().toString());/* 取得选择的性别*/String sex = "";RadioButton rb1 = (RadioButton) findViewById(R.id.Sex_Man);if (rb1.isChecked()) {sex = "M";} else {sex = "F";} /* new 一个Intent 对象,并指定class */Intent intent = new Intent();intent.setClass(Ex10_UI.this, BMIActivity.class);/* new 一个Bundle对象,并将要传递的数据传入*/Bundle bundle = new Bundle();bundle.putDouble("height", height);bundle.putString("sex", sex);/* 将Bundle 对象assign 给Intent */intent.putExtras(bundle);/* 调用Activity EX03_10_1 */startActivity(intent);}});}}
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayoutandroid:id="@+id/widget0"android:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android" ><TextViewandroid:id="@+id/showText"android:layout_width="wrap_content"android:layout_height="26px"android:text="计算你的标准体重!"android:textSize="25px"android:layout_x="65px"android:layout_y="21px"></TextView><TextViewandroid:id="@+id/text_Sex"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="性别:"android:layout_x="71px"android:layout_y="103px"></TextView><TextViewandroid:id="@+id/text_Height"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="身高:"android:layout_x="72px"android:layout_y="169px"></TextView><RadioGroupandroid:id="@+id/radioGroup"android:layout_width="wrap_content"android:layout_height="37px"android:orientation="horizontal"android:layout_x="124px"android:layout_y="101px"><RadioButtonandroid:id="@+id/Sex_Man"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男"></RadioButton><RadioButtonandroid:id="@+id/Sex_Woman"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女"></RadioButton></RadioGroup><EditTextandroid:id="@+id/height_Edit"android:layout_width="123px"android:layout_height="wrap_content"android:text=""android:numeric="decimal"android:textSize="18sp"android:layout_x="124px"android:layout_y="160px"></EditText><Buttonandroid:id="@+id/button_OK"android:layout_width="80px"android:layout_height="wrap_content"android:text="计算"android:layout_x="125px"android:layout_y="263px"></Button></AbsoluteLayout>
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"> <TextViewandroid:id="@+id/text1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:layout_x="50px"android:layout_y="72px"></TextView><Buttonandroid:id="@+id/button_back"android:layout_width="100px"android:layout_height="48px"android:text="回上一页"android:layout_x="110px"android:layout_y="180px"></Button></AbsoluteLayout>
package zyf.Ex11_UI_A;import android.app.Activity;import android.os.Bundle;public class BMIActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="zyf.Ex11_UI_A"android:versionCode="1"android:versionName="1.0"><application android:icon="@drawable/icon"android:label="@string/app_name" ><activity android:name=".Ex11_UI_A"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="BMIActivity"></activity></application><uses-sdk android:minSdkVersion="2" /></manifest>
package zyf.Ex11_UI_A;import android.app.Activity;/* import 相关class */import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.RadioButton;import android.widget.Toast;public class Ex11_UI_A extends Activity {protected int my_requestCode = 1550;private EditText edit_height;private RadioButton radiobutton_Man, radiobutton_Woman;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);/* 载入main.xml Layout */setContentView(R.layout.main);/* 以findViewById()取得Button 对象,并添加onClickListener */Button ok = (Button) findViewById(R.id.button_OK);edit_height = (EditText) findViewById(R.id.height_Edit);radiobutton_Man = (RadioButton) findViewById(R.id.Sex_Man);radiobutton_Woman = (RadioButton) findViewById(R.id.Sex_Woman);ok.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {try {/* 取得输入的身高*/double height = Double.parseDouble(edit_height.getText().toString());/* 取得选择的性别*/String sex = "";if (radiobutton_Man.isChecked()) {sex = "M";} else {sex = "F";}/* new 一个Intent 对象,并指定class */Intent intent = new Intent();intent.setClass(Ex11_UI_A.this, BMIActivity.class);/* new 一个Bundle对象,并将要传递的数据传入*/Bundle bundle = new Bundle();bundle.putDouble("height", height);bundle.putString("sex", sex);/* 将Bundle 对象assign 给Intent */intent.putExtras(bundle);/* 调用Activity EX03_10_1 */startActivityForResult(intent, my_requestCode);} catch (Exception e) {// TODO: handle exceptionToast.makeText(Ex11_UI_A.this,R.string.errorString, Toast.LENGTH_LONG).show();}}});} @Overrideprotected void onActivityResult(int requestCode, int resultCode,Intent data) {// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);switch (resultCode) {case RESULT_OK:/* 取得来自Activity2 的数据,并显示于画面上*/Bundle bunde = data.getExtras();String sex = bunde.getString("sex");double height = bunde.getDouble("height");edit_height.setText("" + height);if (sex.equals("M")) {radiobutton_Man.setChecked(true);} else {radiobutton_Woman.setChecked(true);} break break;default:break;}}}
package zyf.Ex11_UI_A;/* import 相关class */import java.text.DecimalFormat;import java.text.NumberFormat;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;public class BMIActivity extends Activity {private Intent intent;private Bundle bunde;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);/* 加载main.xml Layout */setContentView(R.layout.mylayout);/* 取得Intent 中的Bundle 对象*/intent = this.getIntent();bunde = intent.getExtras();/* 取得Bundle 对象中的数据*/String sex = bunde.getString("sex");double height = bunde.getDouble("height");/* 判断性别*/String sexText = "";if (sex.equals("M")) {sexText = "男性";} else {sexText = "女性";} /* 取得标准体重*/String weight = this.getWeight(sex, height);/* 设置输出文字*/TextView tv1 = (TextView) findViewById(R.id.text1);tv1.setText("你是一位" + sexText + "\n你的身高是" + height +"厘米\n你的标准体重是"+ weight + "公斤");/* 以findViewById()取得Button 对象,并添加onClickListener */Button b1 = (Button) findViewById(R.id.button_back);b1.setOnClickListener(new Button.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub/* 返回result 回上一个activity */BMIActivity.this.setResult(RESULT_OK, intent);/* 结束这个activity */BMIActivity.this.finish();}});} /* 四舍五入的method */private String format(double num) {NumberFormat formatter = new DecimalFormat("0.00");String s = formatter.format(num);return s;} /* 以findViewById()取得Button 对象,并添加onClickListener */private String getWeight(String sex, double height) {String weight = "";if (sex.equals("M")) {weight = format((height - 80) * 0.7);} else {weight = format((height - 70) * 0.6);} return weight;}}