我正在使用此库https://github.com/stepstone-tech/android-material-stepper创建分步注册系统
我有5个片段,每个片段都有一个EditText字段。我如何获取所有写入editText字段中的数据并存储在数据库中(我已经有了使用Volley Library通过JSON和PHP将数据存储到数据库中的方法。我可以通过一个活动和布局来完成此操作,但是我发现很难对片段应用相同的逻辑。)
用户名RegisterFragment
public class UsernameRegisterFragment extends Fragment implements Step {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_username_register, container, false);
//initialize your UI
return v;
}
@Override
public VerificationError verifyStep() {
//return null if the user can go to the next step, create a new VerificationError instance otherwise
return null;
}
@Override
public void onSelected() {
//update UI when selected
}
@Override
public void onError(@NonNull VerificationError error) {
//handle error inside of the fragment, e.g. show error on EditText
}
}
EmailRegisterFragment
public class FullnameRegisterFragment extends Fragment implements Step {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_fullname_register, container, false);
//initialize your UI
return v;
}
@Override
public VerificationError verifyStep() {
//return null if the user can go to the next step, create a new VerificationError instance otherwise
return null;
}
@Override
public void onSelected() {
//update UI when selected
}
@Override
public void onError(@NonNull VerificationError error) {
//handle error inside of the fragment, e.g. show error on EditText
}
}
还有大约4个片段,但它们都是相似的。
IntroActivity.java
这是设置适配器的主要活动,也是用户单击最后一个片段中的最后一个按钮(“完成”按钮)后将调用Register方法的活动。
public class IntroActivity extends AppCompatActivity implements StepperLayout.StepperListener{
private StepperLayout mStepperLayout;
private static String URL_REGISTER = "http://78d24f21.ngrok.io/misnap/register.php";
SessionManager sessionManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
//remove action and status bar
if (getSupportActionBar() != null)
getSupportActionBar().hide();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro);
mStepperLayout = (StepperLayout) findViewById(R.id.stepperLayout);
mStepperLayout.setAdapter(new MyStepperAdapter(getSupportFragmentManager(), this));
mStepperLayout.setListener(this);
}
@Override
public void onCompleted(View completeButton)
{
Register();
}
.......
//This is where I placed the register method
注册方式
public void Register(){
final String username = this.username.getText().toString().trim();
final String email = this.email.getText().toString().trim();
final String password = this.password.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_REGISTER,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success");
if(success.equals("1")){
sessionManager.createSession(username, email);
Intent intent = new Intent(IntroActivity.this, HomeActivity.class);
intent.putExtra("username", username);
intent.putExtra("email", email);
startActivity(intent);
}
} catch (JSONException e){
e.printStackTrace();
Toast.makeText(IntroActivity.this, "Unable to register" + e.toString(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(IntroActivity.this, "Unable to register" + error.toString(), Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("username", username);
params.put("email", email);
params.put("password", password);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
我如何从各个片段中获取ediText数据并将其传递给此寄存器方法,以便可以保存在数据库中?
片段活动的布局只是一个EditText,而简介活动的布局是“下一步”和“后退”按钮。这里是。
activity_intro.xml
<?xml version="1.0" encoding="utf-8"?>
<com.stepstone.stepper.StepperLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/stepperLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:ms_stepperType="progress_bar"
app:ms_nextButtonColor="@color/white"
app:ms_completeButtonColor="@color/white"
app:ms_backButtonColor="@color/white"
/>
启动Activity2 startActivityForResult并使用setResult方法将数据从Activity2发送回Activity1。在活动1,你需要重写onActivityResult用于更新TextView与EditText从活性2数据。
例如:
在Activity1中,将Activity2启动为:
Intent i = new Intent(this, Activity2.class); startActivityForResult(i, 1); 在Activity2中,setResult用于发回数据:
Intent intent = new Intent(); intent.putExtra("editTextValue", "value_here") setResult(RESULT_OK, intent);
finish(); 在Activity1中,通过以下方式接收数据onActivityResult:
public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1) { if(resultCode == RESULT_OK) { String strEditText = data.getStringExtra("editTextValue"); }
} }
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。