Android - 登入系統:創建會員
先在MYSQL建立會員用的表單和之後需要的欄位
Android端:假設MYSQL建立了5個欄位(帳號、密碼、姓名、信箱、電話)
public class register extends AppCompatActivity {/**創建帳號用**/ private EditText editTextName; private EditText editTextUsername; private EditText editTextPassword; private EditText editTextEmail; private EditText editTextTel; private Button buttonRegister; private String REGISTER_URL; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); REGISTER_URL = getString(R.string.register_url); editTextName = (EditText) findViewById(R.id.name); editTextUsername = (EditText) findViewById(R.id.username); editTextPassword = (EditText) findViewById(R.id.password); editTextEmail = (EditText) findViewById(R.id.email); editTextTel= (EditText) findViewById(R.id.tel); buttonRegister = (Button) findViewById(R.id.create); buttonRegister.setOnClickListener(buttonListener); } private Button.OnClickListener buttonListener = new Button.OnClickListener() {/**監聽創見帳號鈕是否被按下**/ @Override public void onClick(View v) { if(v == buttonRegister){ registerUser();/**呼叫這函式進行使用者資料獲取**/ } } }; private void registerUser() {/**讀取使用者輸入數據**/ String name = editTextName.getText().toString().trim().toLowerCase(); String username = editTextUsername.getText().toString().trim().toLowerCase(); String password = editTextPassword.getText().toString().trim().toLowerCase(); String email = editTextEmail.getText().toString().trim().toLowerCase(); String tel = editTextTel.getText().toString().trim().toLowerCase(); register(name,username,password,email,tel);/**獲取資料成功後,開始進行傳送**/ } private void register(String name, String username, String password, String email, String tel) { class RegisterUser extends AsyncTask<String, Void, String> { Createmem ruc = new Createmem();/**使用Creatmem.class的功能**/ @Override protected void onPreExecute() { super.onPreExecute();/**當按下創見鈕,出現提式窗**/ } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show(); if(s.equals("帳號創建成功!"))/**當字串比對成功返回登入頁面**/ { Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show(); Intent intent = new Intent(); intent.setClass(register.this,login.class); startActivity(intent); finish(); } } @Override protected String doInBackground(String... params)/**將資料放入hashmap**/ { HashMap<String, String> data = new HashMap<String,String>(); data.put("name",params[0]); data.put("password",params[1]); data.put("username",params[2]); data.put("email",params[3]); data.put("tel",params[4]); String result = ruc.sendPostRequest(REGISTER_URL,data); return result; } } RegisterUser ru = new RegisterUser();/**傳送資料**/ ru.execute(name, password, username, email,tel); } }Android介面布局
<?xml version="1.0" encoding="utf-8"?> <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:background="@drawable/login_bg" tools:context="project.rmotex.achat.register"> <ImageView android:id="@+id/image" android:background="@drawable/newmem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="70dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <LinearLayout android:orientation="vertical" android:id="@+id/input" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="28.0dip" android:layout_marginRight="28.0dip" android:layout_below="@+id/image"> <EditText android:textSize="16.0sp" android:textColor="#E0E0E0" android:textColorHint="#E0E0E0" android:id="@+id/name" android:background="#40000000" android:paddingLeft="12.0dip" android:layout_width="fill_parent" android:layout_height="44dp" android:maxLines="1" android:maxLength="20" android:hint="Account" android:inputType="text"/> <View android:background="#ffc0c3c4" android:layout_width="fill_parent" android:layout_height="1.0px" android:layout_marginLeft="1.0px" android:layout_marginRight="1.0px" /> <EditText android:textSize="16.0sp" android:textColor="#E0E0E0" android:textColorHint="#E0E0E0" android:hint="Password" android:gravity="center_vertical" android:id="@+id/password" android:background="#40000000" android:paddingLeft="12.0dip" android:layout_width="fill_parent" android:layout_height="44dp" android:maxLines="1" android:maxLength="20" android:inputType="textVisiblePassword" /> <View android:background="#ffc0c3c4" android:layout_width="fill_parent" android:layout_height="1.0px" android:layout_marginLeft="1.0px" android:layout_marginRight="1.0px" /> <EditText android:textSize="16.0sp" android:textColor="#E0E0E0" android:textColorHint="#E0E0E0" android:hint="Name" android:gravity="center_vertical" android:id="@+id/username" android:background="#40000000" android:paddingLeft="12.0dip" android:layout_width="fill_parent" android:layout_height="44dp" android:maxLines="1" android:maxLength="20" android:inputType="textPersonName" /> <View android:background="#ffc0c3c4" android:layout_width="fill_parent" android:layout_height="1.0px" android:layout_marginLeft="1.0px" android:layout_marginRight="1.0px" /> <EditText android:textSize="16.0sp" android:textColor="#E0E0E0" android:textColorHint="#E0E0E0" android:hint="Email" android:gravity="center_vertical" android:id="@+id/email" android:background="#40000000" android:paddingLeft="12.0dip" android:layout_width="fill_parent" android:layout_height="44dp" android:maxLines="1" android:maxLength="30" android:inputType="textEmailAddress" /> <View android:background="#ffc0c3c4" android:layout_width="fill_parent" android:layout_height="1.0px" android:layout_marginLeft="1.0px" android:layout_marginRight="1.0px" /> <EditText android:textSize="16.0sp" android:textColor="#E0E0E0" android:textColorHint="#E0E0E0" android:hint="Telephone" android:gravity="center_vertical" android:id="@+id/tel" android:background="#40000000" android:paddingLeft="12.0dip" android:layout_width="fill_parent" android:layout_height="44dp" android:maxLines="1" android:maxLength="10" android:inputType="phone" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="#40000000" android:text="Create Account" android:textStyle="bold" android:id="@+id/create" android:paddingTop="5.0dip" android:layout_marginLeft="12.0dip" android:layout_marginTop="12.0dip" android:layout_marginRight="12.0dip" android:textSize="20.0sp" /> </LinearLayout> </RelativeLayout>Server端創建會員PHP
<?php /**用於使用者創建帳號**/ if($_SERVER['REQUEST_METHOD']=='POST'){//限制條件為POST $name = $_POST['name'];//將使用者傳的資料存進變數 $username = $_POST['username']; $password = $_POST['password']; $email = $_POST['email']; $tel = $_POST['tel']; if($name == '' || $username == '' || $password == '' || $email == '' || $tel == ''){ echo '請填滿所有選項!'; } else { require_once('dbConnect.php'); $sql = "SELECT * FROM userinfo WHERE name='$name'"; $check = mysqli_fetch_array(mysqli_query($con,$sql)); if(isset($check)) { echo '此帳號已經存在!'; } else { $sql = "INSERT INTO userinfo (name,password,username,email,tel) VALUES('$name','$password','$username','$email','$tel')"; if(mysqli_query($con,$sql)) { echo '帳號創建成功!'; } else { echo '請再嘗試一次!'; } } mysqli_close($con); } } else { echo 'Error'; } ?>
請問Creatmem.class的功能在哪裡?
回覆刪除