The examples in this article share all the codes for Java to implement simple registration and select the city they are located for your reference. The specific content is as follows
1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="username:" /> <EditText android:id="@+id/user" android:minWidth="200px" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:gravity="center_vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="gender:" /> <RadioGroup android:id="@+id/sex" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="male"/> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="woman"/> </RadioGroup> </LinearLayout><LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/textView1" android:text="Please select your city:" android:layout_height="wrap_content" android:layout_width="wrap_content"/> <Spinner android:entries="@array/ctype" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/spinner1"/></LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="password:"/> <EditText android:id="@+id/pwd" android:minWidth="200px" android:inputType="textPassword" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="confirm password:" /> <EditText android:id="@+id/repwd" android:minWidth="200px" android:inputType="textPassword" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="E-mail address:" /> <EditText android:id="@+id/email" android:minWidth="400px" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="submit" /> </LinearLayout>
2.register.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" android:layout_height="match_parent" android:layout_height="wrap_content" android:padding="10px" android:text="username:" /> <TextView android:id="@+id/sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10px" android:text="gender:" /> <TextView android:id="@+id/city" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10px" android:text="city:" /> <TextView android:id="@+id/pwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10px" android:text="password:" /> <TextView android:id="@+id/email" android:padding="10px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="E-mail:" /> <Button android:id="@+id/back" android:text="back to previous step" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
3. MainActivity.java
package com.example.ejcker_llin.myapplication; import android.app.Activity;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.Button;import android.widget.EditText;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Spinner;import android.widget.Toast; public class MainActivity extends Activity { private Button submit; private String sex1; private String city; final int code=0x717; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); submit= (Button) findViewById(R.id.submit); submit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String user=((EditText)findViewById(R.id.user)).getText().toString(); String pwd=((EditText)findViewById(R.id.pwd)).getText().toString(); String repwd=((EditText)findViewById(R.id.repwd)).getText().toString(); String email=((EditText)findViewById(R.id.email)).getText().toString(); RadioGroup sex= (RadioGroup) findViewById(R.id.sex); for(int i=0;i<sex.getChildCount();i++){ RadioButton r= (RadioButton) sex.getChildAt(i); if(r.isChecked()){ sex1=r.getText().toString(); break; } } Spinner spinner= (Spinner) findViewById(R.id.spinner1); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { city=parent.getItemAtPosition(position).toString(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); if(!"".equals(user)&&!"".equals(pwd)&&!"".equals(email)) { if(!pwd.equals(repwd)){ Toast.makeText(MainActivity.this,"The passwords entered twice are inconsistent, please re-enter! ",Toast.LENGTH_LONG).show(); ((EditText) findViewById(R.id.pwd)).setText(""); ((EditText) findViewById(R.id.repwd)).setText(""); ((EditText) findViewById(R.id.pwd)).requestFocus(); }else { Intent intent=new Intent(MainActivity.this,RegisterAcivity.class); Bundle bundle=new Bundle(); bundle.putCharSequence("user",user); bundle.putCharSequence("sex",sex1); bundle.putCharSequence("city",city); bundle.putCharSequence("pwd",pwd); bundle.putCharSequence("email",email); intent.putExtras(bundle); //startActivity(intent); startActivityForResult(intent,code); } }else { Toast.makeText(MainActivity.this,"Please enter the registration information in full!",Toast.LENGTH_LONG).show(); } } }); }} 4. RegisterAcivity.java
package com.example.ejcker_llin.myapplication; import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView; /** * Created by Jcker_llin on 2016/4/5. */public class RegisterAcivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.register); final Intent intent=getIntent(); Bundle bundle=intent.getExtras(); TextView user= (TextView) findViewById(R.id.user); user.setText("Username: "+bundle.getString("user")); TextView sex= (TextView) findViewById(R.id.sex); sex.setText("Gender: "+bundle.getString("sex")); TextView city= (TextView) findViewById(R.id.city); city.setText("City: "+bundle.getString("city")); TextView pwd= (TextView) findViewById(R.id.pwd); pwd.setText("Password: "+bundle.getString("pwd")); TextView email= (TextView) findViewById(R.id.email); email.setText("E-mail: "+bundle.getString("email")); Button button= (Button) findViewById(R.id.back); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setResult(0x717,intent); finish(); } }); }}5.
6.
7. arrays.xml
<?xml version="1.0" encoding="utf-8"?><resources> <string-array name="ctype"> <item>Beijing</item> <item>Shanghai</item> <item>Guangzhou</item> <item>Hangzhou</item> <item>Tianjin</item> <item>Hong Kong</item> <item>Chongqing</item> <item>Xi'an</item> <item>Other</item> </string-array></resources>
The above is all about this article, I hope it will be helpful to everyone's learning.