使用onClick方法从片段中获取edtitext值

时间:2017-12-15 10:26:01

标签: android android-fragments onclick android-edittext fragment

我想创建一个包含Fragment EditText的应用,其中包含三个EditText和一个按钮。我希望按钮在SQLLite中保存EditText的值。目前我只是尝试获取Toast s'值并使用EditText显示它们。

我遇到的问题是我的吐司只打印了我的第一个public class CreatePolicy extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.create_policy); CreatePolicyFragment createPolicyFragment = new CreatePolicyFragment(); if (findViewById(R.id.createPolicy_container) != null) { if (savedInstanceState != null) return; createPolicyFragment.setArguments(getIntent().getExtras()); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.createPolicy_container, createPolicyFragment).commit(); } } } 的值,我不明白为什么我的其他两个值都没有被读取。

这是我的活动代码:

public class CreatePolicyFragment extends Fragment {
    EditText name_choice;
    EditText protocol_choice;
    EditText priority_choice;
    Button button_create;
    Button button_cancel;

    @Override
    public View onCreateView (LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.create_policy_fragment_layout, container, false);

        name_choice = (EditText) view.findViewById(R.id.input_name_choice);
        protocol_choice = (EditText) view.findViewById(R.id.input_protocol_choice);
        priority_choice = (EditText) view.findViewById(R.id.input_priority_choice);
        button_create= view.findViewById(R.id.button_create);

        button_create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String qosName = "";
                String qosProtocol = "";
                int qosPriority = 0;
                qosName = name_choice.getText().toString();
                qosProtocol = protocol_choice.getText().toString();
                qosPriority = Integer.parseInt(priority_choice.getText().toString());
                Toast.makeText(getActivity(), "Policy name : "+ qosName + "\nPolicy protocol : "+ qosProtocol + "\nPolicy priority : "+ qosPriority, Toast.LENGTH_LONG).show();
            }
        });
        return view;
    }
}

我的片段代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <fragment android:name="map.android.com.pushqospolicy.createPolicy.CreatePolicyFragment"
        android:id="@+id/createPolicy_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.constraint.ConstraintLayout>

以下是我的布局:

活动布局:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="map.android.com.pushqospolicy.createPolicy.CreatePolicy"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/input_name_choice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="24dp"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:inputType="text"
        android:labelFor="@+id/input_name_choice"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/qos_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/policy_name"
        app:layout_constraintBottom_toBottomOf="@+id/input_name_choice"
        app:layout_constraintEnd_toStartOf="@+id/input_name_choice"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/input_name_choice" />

    <EditText
        android:id="@+id/input_protocol_choice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="24dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="text"
        android:labelFor="@+id/input_protocol_choice"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/input_name_choice" />

    <TextView
        android:id="@+id/qos_protocol"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/policy_protocol"
        app:layout_constraintBottom_toBottomOf="@+id/input_protocol_choice"
        app:layout_constraintEnd_toStartOf="@+id/input_protocol_choice"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/input_protocol_choice" />

    <EditText
        android:id="@+id/input_priority_choice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="24dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:labelFor="@+id/input_priority_choice"
        android:inputType="number"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/input_protocol_choice" />

    <TextView
        android:id="@+id/qos_priority"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/policy_priority"
        app:layout_constraintBottom_toBottomOf="@+id/input_priority_choice"
        app:layout_constraintEnd_toStartOf="@+id/input_priority_choice"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/input_priority_choice" />

    <Button
        android:id="@+id/button_create"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="24dp"
        android:layout_marginStart="24dp"
        android:text="@string/button_create"
        app:layout_constraintBottom_toTopOf="@+id/button_cancel"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button_cancel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="32dp"
        android:layout_marginEnd="24dp"
        android:layout_marginStart="24dp"
        android:text="@string/button_cancel"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

片段布局:

https://api.instagram.com/v1/tags/%E3%83%99%E3%83%93%E3%83%95%E3%83%AB/media/recent?access_token=ACCESS_TOKEN&count=1&min_tag_id=MIN_TAG_ID

我是Android和片段的初学者......有人可以帮我解决这个问题吗? 非常感谢 ! :)

2 个答案:

答案 0 :(得分:0)

约束布局的问题,我不知道为什么这个问题会出现在约束布局中。我也是新约束布局,会研究并让你更多地了解这个问题。 使用LinearLayout或RelativeLayout来解决这个问题。测试它的工作原理。 对于测试,请找到以下参考。

<?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:orientation="vertical">

    <TextView
        android:id="@+id/qos_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="policy_name" />

    <EditText
        android:id="@+id/input_name_choice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="24dp"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:inputType="text" />


    <TextView
        android:id="@+id/qos_protocol"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="policy_protocol" />

    <EditText
        android:id="@+id/input_protocol_choice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="24dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="text" />

    <TextView
        android:id="@+id/qos_priority"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="policy_priority" />

    <EditText
        android:id="@+id/input_priority_choice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="24dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="number" />


    <Button
        android:id="@+id/button_create"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="24dp"
        android:layout_marginStart="24dp"
        android:text="button_create" />

    <Button
        android:id="@+id/button_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="32dp"
        android:layout_marginEnd="24dp"
        android:layout_marginStart="24dp"
        android:text="button_cancel" />

</LinearLayout>

答案 1 :(得分:0)

问题是您使用+和&#34;&#34;在Toast中附加数据。 只需使用单独的String和来自3 EditText的数据,并将该String传递给toast。

 button_create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String qosName = "";
                String qosProtocol = "";
                int qosPriority = 0;
                qosName = name_choice.getText().toString();
                qosProtocol = protocol_choice.getText().toString();
                qosPriority = Integer.parseInt(priority_choice.getText().toString());
                String data = "PolicyName: " + qosName + "Policy Protocol: "  + qosProtocol  + "Policy Priority: " + qosPriority;
                Toast.makeText(getActivity(), data , Toast.LENGTH_LONG).show();
            }
        });