Android簡單的登錄界麵的值傳遞
初學Android,這是一個用戶登錄界麵,想把裏麵的值傳遞到另一個Activity中,先不說其他,上代碼:
主界麵(登錄界麵的Activity):MainActivity.java
package com.zhoujunwen.widget; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.content.Intent; import android.view.View.OnClickListener; import android.widget.*; public class MainActivity extends Activity { //聲明按鈕Button private Button register,cancle; //聲明ToggleButton private ToggleButton marriged; //聲明單選按鈕 private RadioButton male,female; //聲明文本編輯框 private EditText username,password; //聲明下拉列表 private Spinner position; //聲明多選按鈕 private CheckBox reading,swimming; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //設置Activity頁麵布局 setContentView(R.layout.activity_main); //通過findViewById獲得EditText對象 username = (EditText)findViewById(R.id.username); password = (EditText)findViewById(R.id.password); //通過findViewById方法獲得RadioButton male = (RadioButton)findViewById(R.id.male); female =(RadioButton)findViewById(R.id.female); //通過findViewById獲得ChechBox reading =(CheckBox)findViewById(R.id.reading); swimming =(CheckBox)findViewById(R.id.swimming); //通過findViewById獲得ToggleButton實例 marriged =(ToggleButton)findViewById(R.id.marriged); //通過findViewById獲得Spinner實例 position =(Spinner)findViewById(R.id.position); //下拉列表選項 String[] str = {"CEO","CFO","PM"}; //數組下拉列表適配器 ArrayAdapter aa= new ArrayAdapter(this,android.R.layout.simple_spinner_item,str); //設置下拉列表適配器 position.setAdapter(aa); //通過findViewById方法獲得Button的實例 register = (Button)findViewById(R.id.register); cancle =(Button)findViewById(R.id.cancle); //添加按鈕的單擊事件監聽器 register.setOnClickListener(new OnClickListener(){ //點擊事件方法 @Override public void onClick(View v) { // TODO Auto-generated method stub Bundle b= new Bundle(); //在Bundle中添加用戶名稱和用戶密碼 b.putString("username", "用戶名稱:"+username.getText().toString()); b.putString("password", "用戶密碼:"+password.getText().toString()); //在Bundle中添加性別 if(male.isChecked()){ b.putString("gender","性別:男"); }else{ b.putString("gender","性別:女"); } String temp= "愛好:"; if(reading.isChecked()){ temp+=" "; temp+="閱讀"; } if(swimming.isChecked()) { temp+=" "; temp+="遊泳"; } //在Bundle中添加愛好 b.putString("hobby",temp); //在Bundle中添加婚否 if(marriged.isChecked()){ b.putString("marriged","婚否:已婚"); }else{ b.putString("marriged","婚否:未婚"); } //在Bundle添加職位 b.putString("position","職位:"+position.getSelectedItem().toString()); //實例化Intent,跳轉到ResultActivity Intent intent = new Intent(MainActivity.this,ResultActivity.class); //講Bundle添加到Intent intent.putExtra("data", b); //啟動Activity startActivity(intent); } }); } }分析:1.Spinner製作下拉列表
步驟:①獲取Spinner的實例
②產生下拉列表數組
③數組適配器
④設置下拉列表適配器
代碼:
//通過findViewById獲得Spinner實例 position =(Spinner)findViewById(R.id.position); //下拉列表選項 String[] str = {"CEO","CFO","PM"}; //數組下拉列表適配器 ArrayAdapter aa= new ArrayAdapter(this,android.R.layout.simple_spinner_item,str); //設置下拉列表適配器 position.setAdapter(aa);分析:2.Bundle用於從一個Activity傳值到另一個Activity(其實有很多人說,用Intent的putExtra()方法就可以,但是,Bundle的優點在於傳遞複雜對象的值)
步驟:①實例化Bundle對象,保存屬性
②調用Bundle的putString(key,value)方法,把屬性保存到key裏麵
③實例化Intent,這是Android傳遞值得信使,沒有它不行
④調用Intent的putExtra(key,value),這兒value換成Bundle的實例
⑤啟動該Activity。調用方法startActivity(③中實例化Intent的對象)
代碼:
Bundle b= new Bundle(); //在Bundle中添加用戶名稱和用戶密碼 b.putString("username", "用戶名稱:"+username.getText().toString()); b.putString("password", "用戶密碼:"+password.getText().toString()); //在Bundle中添加性別 if(male.isChecked()){ b.putString("gender","性別:男"); }else{ b.putString("gender","性別:女"); } String temp= "愛好:"; if(reading.isChecked()){ temp+=" "; temp+="閱讀"; } if(swimming.isChecked()) { temp+=" "; temp+="遊泳"; } //在Bundle中添加愛好 b.putString("hobby",temp); //在Bundle中添加婚否 if(marriged.isChecked()){ b.putString("marriged","婚否:已婚"); }else{ b.putString("marriged","婚否:未婚"); } //在Bundle添加職位 b.putString("position","職位:"+position.getSelectedItem().toString()); //實例化Intent,跳轉到ResultActivity Intent intent = new Intent(MainActivity.this,ResultActivity.class); //講Bundle添加到Intent intent.putExtra("data", b); //啟動Activity startActivity(intent);跳轉到的頁麵:ResultActivity.java
package com.zhoujunwen.widget; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class ResultActivity extends Activity{ //聲明ListView private ListView listView; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //設置當前Activity界麵布局 setContentView(R.layout.result); //通過findViewById方法獲得ListView對象 listView=(ListView)findViewById(R.id.ListView01); //獲得Intent Intent intent = this.getIntent(); //從Intent中獲得Bundle Bundle b = intent.getBundleExtra("data"); //實例化List List list = new ArrayList(); //從Bundle中獲得屬性,添加到List list.add(b.getString("username")); list.add(b.getString("password")); list.add(b.getString("position")); list.add(b.getString("gender")); list.add(b.getString("hobby")); list.add(b.getString("marriged")); //實例化數組適配器 ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_checked,list); //為ListView設置適配器 listView.setAdapter(adapter); } }需要分析的地方:Intent獲取傳遞到的值
//獲得Intent Intent intent = this.getIntent(); //從Intent中獲得Bundle Bundle b = intent.getBundleExtra("data"); //實例化List List list = new ArrayList(); //從Bundle中獲得屬性,添加到List list.add(b.getString("username")); list.add(b.getString("password")); list.add(b.getString("position")); list.add(b.getString("gender")); list.add(b.getString("hobby")); list.add(b.getString("marriged")); //實例化數組適配器 ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_checked,list); //為ListView設置適配器 listView.setAdapter(adapter);下麵是兩個布局文件:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TableLayout android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:collapseColumns="3" android:stretchColumns="1"> <TableRow android: android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="用戶名稱" android: android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:text="" android: android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TableRow> <TableRow android: android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="用戶密碼" android: android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:text="" android: android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TableRow> <TableRow android: android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="性別" android: android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <RadioGroup android: android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:text="男" android: android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioButton android:text="女" android: android:layout_width="wrap_content" android:layout_height="wrap_content"/>" </RadioGroup> </TableRow> <TableRow android: android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="婚否" android: android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <ToggleButton android:text="@+id/ToggleButton01" android: android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TableRow> <TableRow android: android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="愛好" android: android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <ChechBox android:text="閱讀" android: android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ChechBox android:text="遊泳" android: android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TableRow> <TableRow android: android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="職務" android: android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <Spinner android: android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TableRow> <TableRow android: android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:text="取消" android: android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:text="注冊" android: android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TableRow> </TableLayout> </LinearLayout>ResultActivity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android: android:layout_width="wrap_content" android:layout_height="wrap_content"/>" </LinearLayout>
最後更新:2017-04-03 12:54:45