如何在片段中使用微调器

时间:2019-02-18 16:54:56

标签: android

我曾尝试在SesionFragment.java中调用微调器,但我不知道该怎么做。

我有以下项目,我希望在SesionFragment.java中调用微调器,并在所选微调器的文本中单击“登录”按钮时,调用另一个活动,例如,选择秘书调用Activity3Main。 Java,并在选择财务主管时调用Activity4Main.java等。

Data Base User, Password and Role

fragment_sesion.xml

<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.josue.login.SesionFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="@string/hello_blank_fragment"
        android:textAlignment="center"
        android:textColor="@color/colorPrimary"
        android:textSize="28sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/user_fragment"
        android:textColor="@color/colorPrimary"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.105"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/textView"
        app:layout_constraintVertical_bias="0.189" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/pwd_fragment"
        android:textColor="@color/colorPrimary"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        app:layout_constraintVertical_bias="0.177" />

    <EditText
        android:id="@+id/txtuser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:ems="10"
        android:hint="Digite su Usuario"
        android:inputType="textPersonName"
        android:textColor="@android:color/holo_blue_bright"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.287"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.178" />

    <EditText
        android:id="@+id/txtpwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:ems="10"
        android:hint="Digite su Contraseña"
        android:inputType="textPassword"
        android:textColor="@android:color/holo_blue_bright"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.741"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtuser"
        app:layout_constraintVertical_bias="0.127" />

    <Button
        android:id="@+id/btnsesion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="172dp"
        android:text="@string/sesion_fragment"
        android:textColor="@android:color/background_dark"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="@+id/textView"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/btnregistrar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/registrar_fragment"
        android:textColor="@android:color/background_dark"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnsesion"
        app:layout_constraintVertical_bias="0.048" />

    <Spinner
        android:id="@+id/simpleSpinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:entries="@array/Cargo"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:textColor="@android:color/holo_blue_bright"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtpwd"
        app:layout_constraintVertical_bias="0.061" />

</android.support.constraint.ConstraintLayout>

SesionFragment.java

package com.example.josue.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
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.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class SesionFragment extends Fragment implements Response.Listener<JSONObject>, Response.ErrorListener {
    RequestQueue rq;
    JsonRequest jrq;
    EditText txtUser, txtPwd;
    Button btnSesion, btnRegistrar;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View vista = inflater.inflate(R.layout.fragment_sesion, container, false);
        txtUser = (EditText) vista.findViewById(R.id.txtuser);
        txtPwd = (EditText) vista.findViewById(R.id.txtpwd);

        btnSesion = (Button) vista.findViewById(R.id.btnsesion);
        btnRegistrar = (Button) vista.findViewById(R.id.btnregistrar);
        rq = Volley.newRequestQueue(getContext());

        btnSesion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iniciar_sesion();
            }
        });

        btnRegistrar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                registrar_usuario();
            }
        });

        // Inflate the layout for this fragment
        return vista;
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(getContext(), "Usuario y/o Contraseña Incorrecto " + txtUser.getText().toString(), Toast.LENGTH_LONG).show();
    }

    @Override
    public void onResponse(JSONObject response) {
        Toast.makeText(getContext(), "Inicio de Sesión Exitoso " + txtUser.getText().toString(), Toast.LENGTH_SHORT).show();
        User usuario = new User();
        JSONArray jsonArray = response.optJSONArray("datos");
        JSONObject jsonObject = null;

        try {
            jsonObject = jsonArray.getJSONObject(0);
            usuario.setUser(jsonObject.optString("user"));
            usuario.setPwd(jsonObject.optString("pwd"));
            usuario.setNames(jsonObject.optString("names"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Intent intencion = new Intent(getContext(), Main2Activity.class);
        intencion.putExtra(Main2Activity.nombres, usuario.getNames());
        startActivity(intencion);
    }
    void iniciar_sesion() {
        String url = "https://xxxxxxxxxx.xxx.xxx/inicio.php?user=" + txtUser.getText().toString() +
                        "&pwd=" + txtPwd.getText().toString();
        jrq = new JsonObjectRequest(Request.Method.GET, url, null, this, this);
        rq.add(jrq);

    }

    void registrar_usuario(){
        RegistrarFragment fr=new RegistrarFragment();
        //fr.setArguments(fr);
        getActivity().getSupportFragmentManager().beginTransaction()
                .replace(R.id.escenario,fr)
                .addToBackStack(null)
                .commit();
    }
}
strings.xml

<resources>
    <string name="app_name">Ipuc Sesion</string>

    <!-- TODO: Remove or change this placeholder text -->
    <string name="hello_blank_fragment">Hermano(a), por favor digite sus datos</string>
    <string name="user_fragment">Usuario:</string>
    <string name="pwd_fragment">Contraseña:</string>
    <string name="sesion_fragment">Iniciar Sesion</string>
    <string name="registrar_fragment">Registrar usuario</string>
    <string name="names_fragment">Nombres: </string>
    <string-array name="Cargo">
        <item>Seleccione su Cargo</item>
        <item>Pastor</item>
        <item>Tesorero(a)</item>
        <item>Secretario(a)</item>
    </string-array>

</resources>

strings.xml

<resources>
    <string name="app_name">Ipuc Sesion</string>

    <!-- TODO: Remove or change this placeholder text -->
    <string name="hello_blank_fragment">Hermano(a), por favor digite sus datos</string>
    <string name="user_fragment">Usuario:</string>
    <string name="pwd_fragment">Contraseña:</string>
    <string name="sesion_fragment">Iniciar Sesion</string>
    <string name="registrar_fragment">Registrar usuario</string>
    <string name="names_fragment">Nombres: </string>
    <string-array name="Cargo">
        <item>Seleccione su Cargo</item>
        <item>Pastor</item>
        <item>Tesorero(a)</item>
        <item>Secretario(a)</item>
    </string-array>

</resources>

Result

1 个答案:

答案 0 :(得分:0)

我使用以下代码解决了这个问题:

package com.example.josue.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
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.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class SesionFragment extends Fragment implements Response.Listener<JSONObject>, Response.ErrorListener {
    RequestQueue rq;
    JsonRequest jrq;
    EditText txtUser, txtPwd;
    Button btnSesion;
    Spinner _spinner;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View vista = inflater.inflate(R.layout.fragment_sesion, container, false);
        txtUser = (EditText) vista.findViewById(R.id.txtuser);
        txtPwd = (EditText) vista.findViewById(R.id.txtpwd);
        _spinner = (Spinner)vista.findViewById(R.id.spinner);
        ArrayAdapter <CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),R.array.Cargo,R.layout.support_simple_spinner_dropdown_item);
        _spinner.setAdapter(adapter);
        btnSesion = (Button) vista.findViewById(R.id.btnsesion);

        rq = Volley.newRequestQueue(getContext());

        btnSesion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iniciar_sesion();
            }
        });



        // Inflate the layout for this fragment
        return vista;
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(getContext(), "Usuario - Contraseña y/o Cargo Incorrecto " + txtUser.getText().toString(), Toast.LENGTH_LONG).show();
    }

    @Override
    public void onResponse(JSONObject response) {
        Toast.makeText(getContext(), "Inicio de Sesión Exitoso " + txtUser.getText().toString(), Toast.LENGTH_SHORT).show();
        User usuario = new User();
        JSONArray jsonArray = response.optJSONArray("datos");
        JSONObject jsonObject = null;

        try {
            jsonObject = jsonArray.getJSONObject(0);
            usuario.setUser(jsonObject.optString("user"));
            usuario.setPwd(jsonObject.optString("pwd"));
            usuario.setNames(jsonObject.optString("names"));
            usuario.setRole(jsonObject.optString("role"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (txtUser.getText().toString().equals("pily") && txtPwd.getText
                ().toString().equals("1234") && _spinner.getSelectedItem().equals("Tesorero(a)")) {

            Intent intencion = new Intent(getContext(), Main2Activity.class);
            intencion.putExtra(Main2Activity.nombres, usuario.getNames());
            startActivity(intencion);

        } else if (txtUser.getText().toString().equals("jair") &&
                txtPwd.getText().toString().equals("1234") && _spinner.getSelectedItem().equals("Secretario(a)")) {

            Intent intencion = new Intent(getContext(), Main3Activity.class);
            intencion.putExtra(Main3Activity.nombres, usuario.getNames());
            startActivity(intencion);

        } else if (txtUser.getText().toString().equals("jose") &&
                txtPwd.getText().toString().equals("12345") && _spinner.getSelectedItem().equals("Pastor")) {

            Intent intencion = new Intent(getContext(), Main4Activity.class);
            intencion.putExtra(Main4Activity.nombres, usuario.getNames());
            startActivity(intencion);

        } else {
            Toast.makeText(getContext(), "Usuario - Contraseña y/o Cargo Incorrecto " + txtUser.getText().toString(), Toast.LENGTH_LONG).show();
        }
    }
    void iniciar_sesion() {
        String url = "https://ipucsegundaalgarrobo.000webhostapp.com/inicio.php?user=" + txtUser.getText().toString() +
                "&pwd=" + txtPwd.getText().toString() + "&role=" + _spinner.getSelectedItem().toString();

        jrq = new JsonObjectRequest(Request.Method.GET, url, null, this, this);
        rq.add(jrq);
    }


}