主畫面配置幾個Button控制項,每個Button按下去以後顯示另外的畫面呈現另外的基本控制項。
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".AppMain">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollView" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕控制項"
android:id="@+id/btn_Button"
android:layout_below="@+id/textView"
android:layout_toEndOf="@+id/scrollView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顯示文字控制項"
android:id="@+id/btn_TextView"
android:layout_below="@+id/button"
android:layout_toEndOf="@+id/scrollView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="編輯文字控制項"
android:id="@+id/btn_Edit_Text"
android:layout_below="@+id/button2"
android:layout_toEndOf="@+id/scrollView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="勾選式控制項"
android:id="@+id/btn_Check_Box" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="點選式控制項"
android:id="@+id/btn_Raid_Grop" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
AppMain.java
package com.example.win7.button_activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class AppMain extends Activity {
private Button btn_Button,btn_TextView,btn_Edit_Text,btn_Check_Box,btn_Raid_Grop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViewComponent();
}
private void setupViewComponent(){
btn_Button=(Button)findViewById(R.id.btn_Button);
btn_TextView=(Button)findViewById(R.id.btn_TextView);
btn_Edit_Text=(Button)findViewById(R.id.btn_Edit_Text);
btn_Check_Box=(Button)findViewById(R.id.btn_Check_Box);
btn_Raid_Grop=(Button)findViewById(R.id.btn_Raid_Grop);
btn_Button.setOnClickListener(Press_Button);
btn_TextView.setOnClickListener(Press_TexView);
btn_Edit_Text.setOnClickListener(Press_EditText);
btn_Check_Box.setOnClickListener(Press_CheckBox);
btn_Raid_Grop.setOnClickListener(Press_Raid_Grop);
}
private Button.OnClickListener Press_Button = new Button.OnClickListener(){
@Override
public void onClick(View view) {
/*Andioid切換畫面是用Intentj物件*/
Intent intent_Button = new Intent();
/*使用Intent的setclass()來設定目前以及將轉換的Activity*/
intent_Button.setClass(AppMain.this,SampleButton.class);
/*startActivity()方法將剛剛設定好的Intent傳遞進去*/
startActivity(intent_Button);
}
};
private Button.OnClickListener Press_TexView = new Button.OnClickListener(){
@Override
public void onClick(View view) {
Intent intent_TextView = new Intent();
intent_TextView.setClass(AppMain.this,SampleTextView.class);
startActivity(intent_TextView);
}
};
private Button.OnClickListener Press_EditText = new Button.OnClickListener(){
@Override
public void onClick(View view) {
Intent intent_EdistText = new Intent();
intent_EdistText.setClass(AppMain.this,SampleEditText.class);
startActivity(intent_EdistText);
}
};
private Button.OnClickListener Press_Raid_Grop = new Button.OnClickListener(){
@Override
public void onClick(View view) {
Intent intent_RaidGrop = new Intent();
intent_RaidGrop.setClass(AppMain.this,SampleRaidGrop.class);
startActivity(intent_RaidGrop);
}
};
private Button.OnClickListener Press_CheckBox = new Button.OnClickListener(){
@Override
public void onClick(View view) {
Intent intent_CheckBox = new Intent();
intent_CheckBox.setClass(AppMain.this,SampleCheckBox.class);
startActivity(intent_CheckBox);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_app_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
sample_button.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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="點我點我"
android:id="@+id/btn_Click" />
</LinearLayout>
SampleButton.java
package com.example.win7.button_activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
* Created by win7 on 2015/2/15.
*/
public class SampleButton extends Activity {
private Button btn_Click;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_button);
setupViewComponent();
}
private void setupViewComponent() {
btn_Click = (Button) findViewById(R.id.btn_Click);
btn_Click.setOnClickListener(ClickButton);
}
private Button.OnClickListener ClickButton = new Button.OnClickListener() {
@Override
public void onClick(View view) {
setTitle("Button以被點選");
}
};
}
sample_edit_text.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">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="352dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:id="@+id/txv_Name" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edt_Name"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取得EditView中的內容"
android:id="@+id/btn_Get_Edit_Text" />
</LinearLayout>
</LinearLayout>
SampleEditText.java
package com.example.win7.button_activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* Created by win7 on 2015/2/15.
*/
public class SampleEditText extends Activity {
private Button btn_Get_Edit_Text;
private TextView txv_Name;
private EditText edt_Name;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_edit_text);
setupViewComponent();
}
private void setupViewComponent() {
btn_Get_Edit_Text = (Button) findViewById(R.id.btn_Get_Edit_Text);
txv_Name = (TextView) findViewById(R.id.txv_Name);
edt_Name = (EditText) findViewById(R.id.edt_Name);
btn_Get_Edit_Text.setOnClickListener(Get_EditText);
}
private Button.OnClickListener Get_EditText = new Button.OnClickListener() {
@Override
public void onClick(View view) {
setTitle("輸入的名稱為:"+edt_Name.getText());
}
};
}
sample_check_box.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">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apple"
android:id="@+id/cb_Apple" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Movie"
android:id="@+id/cb_Movie" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TV"
android:id="@+id/cb_TV" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取得目前被選擇的值"
android:id="@+id/btn_Select"/>
</LinearLayout>
</LinearLayout>
SampleCheckBox.java
package com.example.win7.button_activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
/**
* Created by win7 on 2015/2/15.
*/
public class SampleCheckBox extends Activity {
private CheckBox cb_Apple,cb_Movie,cb_TV;
private Button btn_Select;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_check_box);
setTitle("這是CheckBox的Activity");
setupViewComponent();
}
private void setupViewComponent(){
cb_Apple=(CheckBox)findViewById(R.id.cb_Apple);
cb_Movie=(CheckBox)findViewById(R.id.cb_Movie);
cb_TV=(CheckBox)findViewById(R.id.cb_TV);
btn_Select=(Button)findViewById(R.id.btn_Select);
btn_Select.setOnClickListener(Check_Select);
}
private Button.OnClickListener Check_Select = new Button.OnClickListener(){
public void onClick(View view){
String s_Checked="";
/*接字串符號+=*/
if(cb_Apple.isChecked()){
s_Checked+=cb_Apple.getText()+",";
}
if(cb_Movie.isChecked()){
s_Checked+=cb_Movie.getText()+",";
}
if(cb_TV.isChecked()){
s_Checked+=cb_TV.getText()+",";
}
setTitle("Checked:"+s_Checked);
}
};
}
sample_radio_group.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">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RadioGroup
android:id="@+id/rdg_Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apple"
android:id="@+id/rdg_Apple" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Movie"
android:id="@+id/rdg_Movie" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TV"
android:id="@+id/rdg_TV" />
</RadioGroup>
</LinearLayout>
</LinearLayout>
SampleRadioGroup.java
package com.example.win7.button_activity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
/**
* Created by win7 on 2015/2/15.
*/
public class SampleRaidGrop extends Activity {
/*宣告一個控制項,和三個控制項*/
private RadioGroup rdg_Main;
private RadioButton rdg_Apple,rdg_Movie,rdg_TV;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_radio_group);
setTitle("這是RadioButton的Activity");
setupViewComponent();
}
private void setupViewComponent(){
rdg_Main=(RadioGroup)findViewById(R.id.rdg_Main);
rdg_Apple=(RadioButton)findViewById(R.id.rdg_Apple);
rdg_Movie=(RadioButton)findViewById(R.id.rdg_Movie);
rdg_TV=(RadioButton)findViewById(R.id.rdg_TV);
rdg_Main.setOnCheckedChangeListener(Check_Radio);
}
/*這裡特別注意RadipGroup所使用的事件是OnCheckedChangeListener*/
private RadioGroup.OnCheckedChangeListener Check_Radio = new RadioGroup.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if(i==rdg_Apple.getId()){
setTitle("我的喜好:"+rdg_Apple.getText());
}
else if(i==rdg_Movie.getId()){
setTitle("我的喜好:"+rdg_Movie.getText());
}
else if(i==rdg_TV.getId()){
setTitle("我的喜好:"+rdg_TV.getText());
}
}
};
}
沒有留言:
張貼留言