Android微调器实现未显示所选项

时间:2017-08-02 01:25:21

标签: android spinner

我有3个Spinners,我从每个人中选择值。但是当我在下一个按钮的onClickListener()之外声明setOnItemSelected()方法时,所选值不会显示在Toast中。当我在按钮的onClickListener中声明setOnItemSelected()方法时,它可以工作,但是当我从上一个微调器中选择“Set Limit”时,我无法隐藏我的edittext。 请帮忙。 下面是我的.java文件。

public class AvailabilityActivity extends AppCompatActivity {

private Button next;
private Spinner advanceNotice, shortestTrip, longestDist;
private Bundle bundle;
private EditText setLimit;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_availability);

    bundle = getIntent().getExtras();
    intializeSpinners();
    setLimit = (EditText) findViewById(R.id.setlimit);
    ArrayAdapter<CharSequence> adapteradvNotice = ArrayAdapter.createFromResource(this, R.array.advNoticeArray, R.layout.spinner_layout);
    adapteradvNotice.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    advanceNotice.setAdapter(adapteradvNotice);

    ArrayAdapter<CharSequence> adaptershortestTrip = ArrayAdapter.createFromResource(this, R.array.shortestTripArray, R.layout.spinner_layout);
    adaptershortestTrip.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    shortestTrip.setAdapter(adaptershortestTrip);

    ArrayAdapter<CharSequence> adapterlongestDist = ArrayAdapter.createFromResource(this, R.array.longestDistanceArray, R.layout.spinner_layout);
    adapterlongestDist.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    longestDist.setAdapter(adapterlongestDist);
    onNextPressed();
}

private void intializeSpinners() {
    advanceNotice = (Spinner) findViewById(R.id.acceptAdvanceNotice);
    shortestTrip = (Spinner) findViewById(R.id.acceptShortestTrip);
    longestDist = (Spinner) findViewById(R.id.acceptLongestTrip);
}

private void onNextPressed() {
    next = (Button) findViewById(R.id.nextButton1);

    final String[] setLimitText = {""};
    final String[] selectedlongestDist = new String[1];
    longestDist.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            selectedlongestDist[0] = parent.getItemAtPosition(position).toString();
            if (selectedlongestDist[0].equals("Set Limit")){
                setLimit.setVisibility(View.VISIBLE);
                setLimitText[0] = setLimit.getText().toString();
            }
            if (selectedlongestDist[0].equals("No Limit")) {
                setLimit.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    if (setLimitText[0] == "")
        setLimitText[0] = selectedlongestDist[0];


    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String[] selectedNotice = new String[1];
            advanceNotice.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    selectedNotice[0] = parent.getItemAtPosition(position).toString();
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

            final String[] selectedshortestTrip = new String[1];
            shortestTrip.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    selectedshortestTrip[0] = parent.getItemAtPosition(position).toString();
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

            Toast.makeText(getApplicationContext(), setLimitText[0], Toast.LENGTH_LONG).show();

            bundle.putString("advancenotice", selectedNotice[0]);
            bundle.putString("shortesttrip", selectedshortestTrip[0]);
            bundle.putString("longesttrip", setLimitText[0]);

            Intent intent = new Intent(getBaseContext(),ImageActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    });
}
}

下面是我的布局文件。

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.csci567.dailyrentals.AvailabilityActivity">


<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="How much advance notice do you need to confirm a trip request?"
    android:textColor="#000000"
    android:textSize="18dp"
    android:layout_marginLeft="15dp"
    android:id="@+id/advanceNoticeRequestText"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintVertical_bias="0.035"
    android:layout_marginStart="15dp" />

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Advance notice"
    android:textSize="14dp"
    android:id="@+id/advanceNoticeText"
    android:layout_marginLeft="15dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintVertical_bias="0.13"
    android:layout_marginStart="15dp" />

<Spinner
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/acceptAdvanceNotice"
    android:hint="Advance Notice"
    style="@style/Widget.AppCompat.Spinner.Underlined"
    android:layout_marginLeft="15dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintVertical_bias="0.18"
    android:layout_marginStart="15dp" />

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Block trips that don't give you enough advance notice."
    android:textSize="16dp"
    android:id="@+id/blockNoticeText"
    android:layout_marginLeft="15dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintVertical_bias="0.26"
    android:layout_marginStart="15dp" />

<View android:background="#a8a8a6"
    android:layout_width = "0dp"
    android:layout_height="1dp"
    android:id="@+id/separatorLine1"
    android:layout_marginTop="25dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintVertical_bias="0.3"/>

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="How long would you like trips to last?"
    android:textSize="18dp"
    android:textColor="#000000"
    android:id="@+id/tripDurationText"
    android:layout_marginLeft="15dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintVertical_bias="0.4"
    android:layout_marginStart="15dp" />

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Shortest possible trip"
    android:textSize="14dp"
    android:id="@+id/shortestTripText"
    android:layout_marginLeft="15dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintVertical_bias="0.46"
    android:layout_marginStart="15dp" />

<Spinner
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/acceptShortestTrip"
    android:hint="Enter Shortest Trip"
    style="@style/Widget.AppCompat.Spinner.Underlined"
    android:layout_marginLeft="15dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintVertical_bias="0.52"
    android:layout_marginStart="15dp" />

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Longest Possible trip"
    android:textSize="14dp"
    android:id="@+id/longestTripText"
    android:layout_marginLeft="15dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintVertical_bias="0.6"
    android:layout_marginStart="15dp" />

<Spinner
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/acceptLongestTrip"
    android:hint="Enter Longest Trip"
    style="@style/Widget.AppCompat.Spinner.Underlined"
    android:layout_marginLeft="15dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintVertical_bias="0.66"
    android:layout_marginStart="15dp" />

<EditText
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/setlimit"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintVertical_bias="0.76"
    android:hint="Enter the value of longest possible trip"
    android:layout_marginStart="15dp"
    android:layout_marginLeft="15dp" />

<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="35dp"
    android:text="Next"
    android:textSize="18dp"
    android:textColor="#ffffff"
    android:background="#8b36bc"
    android:id="@+id/nextButton1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintVertical_bias="1.0"/>

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:1)

你编码的方式就是问题。

你必须改变的第一件事是你设置听众的地方。您应该避免在其他侦听器中设置侦听器。

其次,代码将执行这些侦听器中的所有代码,然后在这些侦听器中执行代码。

第三,如果你想要在Toast中显示一条消息,你想把它显示在Spinner中,你应该将它放在微调器的监听器中。

尝试重新组织您考虑这些提示的代码,看看您是否会再次遇到此问题。