安卓用户注册案例与Activity数据传递
在安卓开发中,经常需要Activity中进行数据传递,这里需要用Intent还实现Activity之间数据传递。
·
在安卓开发中,经常需要Activity中进行数据传递,这里需要用Intent还实现Activity之间数据传递。
使用Intent进行数据传递时只需要调用putExtra()方法把数据储存进去即可。这个方法有两个参数,是一种“键值对”的形式,第一个参数为key,第二个参数是value。
实现传递数据的具体代码如下:
//定义字符串变量储存一个值
String str="android";
Intent intent=new Intent(this,SecondAtivity.class);
//传递参数
intent.putExtra("receive_str",str);
startActivity(intent);
上述代码中将一个字符串变量 str 传递到 SecondActivity 中,然后需要在 SecondActivity中接收这个参数,具体的代码如下所示
Intent intent=this.getIntent();
String receive _ str = intent . getStringExtra (" receive _ st ");
上面就是通过 Intent 传递和接收参数的一种简单方式,如果需要传递的参数比较多,就需要使用 putExtras ()方法传递数据,该方法传递的是 Bundle 对象,具体的代码如下所示:
Intent intent = new Intent ( this,SecondActivity.class );
Bundle bundle = new Bundle ();
bundle.putString("phone","123456");
bundle.putString("sex","男");
bundle.putString("age","18");
intent.putExtras(bundle);
startActivity(intent);
上述代码使用 Bundle 对象传递参数,在 SecondActivity 中取出这些参数的具体代码如下所示:
Intent intent = this.getIntent ();
Bundle bundle = intent.getExtras();
String phone = bundle.getString("phone")
在上述代码中,在接收 Bundle 对象封装的数据时,需要先接收对应的 Bundle 对象,然后再根据key取出value。
1.注册界面设计
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.activity.MainActivity">
<LinearLayout
android:layout_width="368dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="0dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="手机号:"
android:textColor="@android:color/background_dark"
android:textSize="18sp" />
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="请输入手机号"
android:inputType="number"/>
</LinearLayout>
<LinearLayout
android:layout_width="368dp"
android:layout_height="wrap_content"
tools:layout_editor_absoluteY="0dp"
android:orientation="horizontal"
tools:layout_editor_absoluteX="8dp">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:textSize="18sp"
android:textColor="@android:color/background_dark"
android:text="密 码:" />
<EditText
android:id="@+id/paswd"
android:layout_width="match_parent"
android:layout_height="50dp"
android:ems="10"
android:inputType="textPassword"
android:hint="请输入密码" />
</LinearLayout>
<RadioGroup
android:id="@+id/sex"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/nan"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/nv"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:checked="true"
android:text="女" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="玩游戏" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读书" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="听音乐" />
</LinearLayout>
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/register"
android:textSize="18sp"
android:textColor="#FFFFFF"
android:layout_width="fill_parent"
android:background="#3F51B5"
android:layout_height="40dp"
android:text="注册" />
</LinearLayout>
数据显示界面设计
<?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">
<TextView
android:id="@+id/show_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
MainActivity代码如下:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import androidx.annotation.IdRes;
public class MainActivity extends Activity implements View.OnClickListener,RadioGroup.OnCheckedChangeListener{
private String phone_str=" ";
private String paswd_str=" ";
//默认为女性被选中
private String sex_str="女";
private String hobby_str="1";
private String city_str="";
EditText phone_edit,paswd_edit;
RadioGroup sex_group;
RadioButton nan_but,nv_but;
CheckBox play,read,music;
Button register;
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
phone_edit=(EditText)findViewById(R.id.phone) ;
paswd_edit=(EditText)findViewById(R.id.paswd);
sex_group=(RadioGroup)findViewById(R.id.sex);
sex_group.setOnCheckedChangeListener((RadioGroup.OnCheckedChangeListener) this);
nv_but=(RadioButton)findViewById(R.id.nv);
read=(CheckBox)findViewById(R.id.checkBox);
play=(CheckBox)findViewById(R.id.checkBox2);
music=(CheckBox)findViewById(R.id.checkBox3);
register=(Button)findViewById(R.id.register);
register.setOnClickListener(this);
Spinner spinner=(Spinner)findViewById(R.id.spinner);
String[] city= new String[]{"北京","上海","青海","长沙","南京","武汉"};
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,city);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
city_str=city[i];
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.register:
phone_str=phone_edit.getText().toString();
paswd_str=paswd_edit.getText().toString();
hobby_str="";
if(read.isChecked()){
hobby_str+=read.getText().toString();
}if(play.isChecked()){
hobby_str+=play.getText().toString();
}if(music.isChecked()){
hobby_str+=music.getText().toString();
}
Intent intent=new Intent(this,SeActivity.class);
Bundle bundle=new Bundle();
bundle.putString("phone",phone_str);
bundle.putString("paswd",paswd_str);
bundle.putString("sex",sex_str);
bundle.putString("hobby",hobby_str);
bundle.putString("city",city_str);
intent.putExtras(bundle);
startActivity(intent);
break;
}
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
sex_str=i==R.id.nv?"男性":"女性";
}
}
SecondActivity代码如下:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SeActivity extends Activity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
Intent intent=this.getIntent();
Bundle bundle=intent.getExtras();
String phone=bundle.getString("phone");
String paswd=bundle.getString("paswd");
String sex=bundle.getString("sex");
String hobby=bundle.getString("hobby");
String city=bundle.getString("city");
TextView show_text=(TextView)findViewById(R.id.show_content);
show_text.setText("手机号:"+phone+"\n"+"密码:"+paswd+"\n"+"性别:"+sex+"\n"
+"爱好:"+hobby+"\n"+"城市:"+city);
}
}
在 AndroidManofest . xml 文件中注册 SecondActivity ,代码如下所示:
< activity android:name=".SecondActivity"></activity >
更多推荐
已为社区贡献4条内容
所有评论(0)