需要帮助。当用户填写他们的数据并保存到数据库时,我希望按钮在保存后打开新活动...到目前为止,我的应用程序刚刚保存了数据并且只停留在activity_form.xml.need
帮助上如何如果有人可以帮我解决这个问题,那么在保存数据后让提交按钮打开新活动会非常感激。 TQ
这是我的formActivity.java
:
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Patterns;
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 android.widget.Toast;
import android.view.View.OnClickListener;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FormActivity extends Activity implements View.OnClickListener, AdapterView.OnItemSelectedListener {
private static final String REGISTER_URL = "http://khaty-ismail0.rhcloud.com/phptutorial/submitData.php";
private static final String SPINNER_URL = "http://khaty-ismail0.rhcloud.com/phptutorial/spinner_data.php";
public static final String KEY_NAME= "Name";
public static final String KEY_AGE = "Age";
public static final String KEY_EMAIL = "Email";
public static String KEY_GENDER = "gender";
public static final String KEY_PHONE = "Phone";
public static final String KEY_DESCRIPTION = "Description";
public static final String KEY_TICKET = "ticket_id";
public static final String KEY_SOLUTION = "solution";
public static final String JSON_ARRAY = "result";
String gender ="";
private EditText name_eT;
private EditText age_eT;
private EditText email_eT;
private EditText phone_eT;
private EditText descr_eT;
private RadioGroup radioGroup;
private Button subm_btn;
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
name_eT = (EditText) findViewById(R.id.name_eT);
age_eT = (EditText) findViewById(R.id.age_eT);
email_eT= (EditText) findViewById(R.id.email_eT);
phone_eT= (EditText) findViewById(R.id.phone_eT);
descr_eT= (EditText) findViewById(R.id.descr_eT);
subm_btn = (Button) findViewById(R.id.subm_btn);
subm_btn.setOnClickListener(this);
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), spinner.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void registerUser(){
final String Name = name_eT.getText().toString().trim();
final String Age = age_eT.getText().toString().trim();
final String Email = email_eT.getText().toString().trim();
final String Phone = phone_eT.getText().toString().trim();
final String Description = descr_eT.getText().toString().trim();
if(TextUtils.isEmpty(Name)) {
name_eT.setError("Please enter your name");
return;
}else if(TextUtils.isEmpty(Age)) {
age_eT.setError("Please enter your age");
return;
}else if(TextUtils.isEmpty(Phone)) {
phone_eT.setError("Please enter your phone");
return;
}else if(TextUtils.isEmpty(Email)) {
email_eT.setError("Please enter your email");
return;
}else if(TextUtils.isEmpty(Description)) {
descr_eT.setError("Please enter your issue");
return;
}
final RadioGroup radioGroup =(RadioGroup)findViewById(R.id.radioGroup);
final RadioButton male_rb=(RadioButton)findViewById(R.id.male_rb);
final RadioButton female_rb=(RadioButton)findViewById(R.id.female_rb);
if (radioGroup.getCheckedRadioButtonId()== male_rb.getId())
{
KEY_GENDER = "Male";
}
else if (radioGroup.getCheckedRadioButtonId()==female_rb.getId()){
KEY_GENDER = "Female";
}
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(FormActivity.this,response,Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(FormActivity.this,error.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put(KEY_NAME,Name);
params.put(KEY_AGE,Age);
params.put(KEY_GENDER,gender);
params.put(KEY_EMAIL, Email);
params.put(KEY_PHONE, Phone);
params.put(KEY_DESCRIPTION, Description);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
public void onCheckboxClicked(View V) {
boolean checked = ((RadioButton) V).isChecked();
switch (V.getId()) {
case R.id.male_rb:
if (checked)
break;
case R.id.female_rb:
if (checked)
break;
}
}
@Override
public void onClick(View v) {
if(v == subm_btn){
registerUser();
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
/*public void submitData(View view) {
Intent getResponse = new Intent(this,SubmitData.class);
final int result = 1;
startActivityForResult(getResponse, result);
}*/
}
以下是activity_form.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/buttonFont"
tools:context="com.example.khadijah.brucertv2.FormActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/User_info"
android:id="@+id/userInfo"
android:textSize="30sp"
android:textColor="@color/colorFont"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="Name:"
android:id="@+id/name_tV"
android:textColor="@color/colorFont"
android:layout_marginTop="24dp"
android:layout_below="@+id/userInfo"
android:layout_alignLeft="@+id/userInfo"
android:layout_alignStart="@+id/userInfo" />
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="Age:"
android:id="@+id/age_tV"
android:textColor="@color/colorFont"
android:layout_alignBottom="@+id/age_eT"
android:layout_alignLeft="@+id/name_tV"
android:layout_alignStart="@+id/name_tV" />
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="Gender"
android:id="@+id/gender_tV"
android:textColor="@color/colorFont"
android:layout_below="@+id/age_tV"
android:layout_alignLeft="@+id/age_tV"
android:layout_alignStart="@+id/age_tV"
android:layout_marginTop="24dp" />
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="Email:"
android:id="@+id/email_tV"
android:textColor="@color/colorFont"
android:layout_alignBottom="@+id/email_eT"
android:layout_alignLeft="@+id/name_tV"
android:layout_alignStart="@+id/name_tV" />
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="Phone"
android:id="@+id/phone_tV"
android:textColor="@color/colorFont"
android:layout_alignBottom="@+id/phone_eT"
android:layout_alignLeft="@+id/gender_tV"
android:layout_alignStart="@+id/gender_tV" />
<EditText
android:layout_width="200dp"
android:layout_height="30dp"
android:layout_marginBottom="5dp"
android:inputType="textPersonName"
android:hint="@string/hint_required"
android:textColorHint="@color/buttonFont"
android:background="@drawable/border_style"
android:ems="10"
android:id="@+id/name_eT"
android:textColor="@color/inputFont"
android:layout_alignBottom="@+id/name_tV"
android:layout_alignRight="@+id/userInfo"
android:layout_alignEnd="@+id/userInfo" />
<EditText
android:layout_width="200dp"
android:layout_height="30dp"
android:inputType="number"
android:ems="10"
android:hint="@string/hint_required"
android:textColorHint="@color/buttonFont"
android:background="@drawable/border_style"
android:id="@+id/age_eT"
android:textColor="@color/inputFont"
android:layout_below="@+id/name_tV"
android:layout_alignRight="@+id/userInfo"
android:layout_alignEnd="@+id/userInfo" />
<EditText
android:layout_width="200dp"
android:layout_height="30dp"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/email_eT"
android:hint="@string/hint_required"
android:textColorHint="@color/buttonFont"
android:textColor="@color/inputFont"
android:background="@drawable/border_style"
android:layout_below="@+id/phone_eT"
android:layout_alignLeft="@+id/age_eT"
android:layout_alignStart="@+id/age_eT" />
<EditText
android:layout_width="200dp"
android:layout_height="30dp"
android:inputType="phone"
android:ems="10"
android:id="@+id/phone_eT"
android:layout_marginBottom="5dp"
android:textColor="@color/inputFont"
android:hint="@string/hint_required"
android:textColorHint="@color/buttonFont"
android:background="@drawable/border_style"
android:layout_below="@+id/gender_tV"
android:layout_alignLeft="@+id/email_eT"
android:layout_alignStart="@+id/email_eT" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/incident_info"
android:id="@+id/incidentInfo"
android:textColor="@color/colorFont"
android:textStyle="bold"
android:textSize="30sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:prompt="@array/incident_type"
android:entries="@array/incident_type"
android:layout_below="@+id/incidentInfo"
android:layout_alignLeft="@+id/phone_eT"
android:layout_alignStart="@+id/phone_eT"
android:layout_alignRight="@+id/phone_eT"
android:layout_alignEnd="@+id/phone_eT"
android:spinnerMode="dropdown" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/incident_type_DD"
android:textColor="@color/colorFont"
android:id="@+id/incident_DD"
android:layout_below="@+id/incidentInfo"
android:layout_toLeftOf="@+id/spinner"
android:layout_toStartOf="@+id/spinner" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/description"
android:id="@+id/description_tV"
android:textColor="@color/colorFont"
android:layout_below="@+id/spinner"
android:layout_alignLeft="@+id/phone_tV"
android:layout_alignStart="@+id/phone_tV" />
<EditText
android:layout_width="300dp"
android:layout_height="80dp"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/descr_eT"
android:hint="@string/descr_hint"
android:textColorHint="@color/buttonFont"
android:gravity="top"
android:background="@drawable/border_style"
android:textColor="@color/inputFont"
android:layout_below="@+id/description_tV"
android:layout_alignLeft="@+id/description_tV"
android:layout_alignStart="@+id/description_tV" />
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="5dp"
android:text="SUBMIT"
android:onClick="submitData"
android:textStyle="bold"
android:textSize="35sp"
android:id="@+id/subm_btn"
android:background="@drawable/custom_btn"
android:textColor="@color/buttonFont"
android:layout_alignParentBottom="true"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignLeft="@+id/phone_eT"
android:layout_alignStart="@+id/phone_eT"
android:layout_below="@+id/age_eT"
android:layout_alignRight="@+id/descr_eT"
android:layout_alignEnd="@+id/descr_eT"
android:layout_above="@+id/phone_eT"
android:weightSum="1"
android:id="@+id/radioGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="Male"
android:id="@+id/male_rb"
android:onClick="onCheckboxClicked"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:checked="false"
android:id="@+id/female_rb"
android:onClick="onCheckboxClicked"/>
</RadioGroup>
</RelativeLayout>
我希望提交按钮保存并打开另一个名为submit_data.xml
的活动:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/buttonFont"
android:padding="16dp"
tools:context="com.example.khadijah.brucertv2.SubmitData">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Thank You for reporting to BruCERT."
android:id="@+id/text1"
android:layout_gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="You will be entertained within 24hours. Your information will be treated strictly confidential."
android:id="@+id/text2"
android:layout_below="@+id/text1"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Please retain the ticket number for quick follow up and to ensure effective response."
android:id="@+id/text3"
android:layout_below="@+id/text2"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp" />
</RelativeLayout>
以下是submitData.java
:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
/**
* Created by Khadijah on 3/29/2016.
*/
public class SubmitData extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.submit_data);
}
public void submitData(View view) {
Intent getResponse = new Intent(this,SubmitData.class);
final int result = 1;
startActivityForResult(getResponse, result);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.khadijah.brucertv2">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/brucertlogotrans"
android:supportsRtl="true"
android:theme="@android:style/Theme.Holo.Light">
<activity
android:name=".splash"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="com.example.khadijah.brucertv2.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>-->
</activity>
<activity
android:name=".FormActivity"
android:label="Online Form">
<intent-filter>
<action android:name="com.example.khadijah.brucertv2.FormActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".SubmitData"
android:label="Feedback">
<intent-filter>
<action android:name="com.example.khadijah.brucertv2.SubmitData" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
添加此代码
startActivity(new Intent(FormActivity.this, SubmitData.class));
在 onResponse(字符串响应)方法中。并从SubmitData.java文件中删除submitData方法。同时从activity_form.xml文件中删除 android:onClick =&#34; submitData&#34; 。
答案 1 :(得分:0)
我会使登录按钮调用AsyncTask,并在验证信息后的onPostExecute()上,你可以将startActivity()调用到你要打开的任何Activity。