spinner
をポップアップウィンドウ内に実装しようとしましたが、OnItemselection
のspinner
アイテムが選択されていませんでした。このシナリオでは2種類の実験的テストがあります。SpinnerのPopUpウィンドウが選択されていない
実験テスト-1私は、OnItemSelectionListener
を実施してきた実験テスト-1の結果は、
java.lang.NullPointerExceptionがある:「無効 android.widget.Buttonを仮想メソッドを呼び出すための試み。 'nullのオブジェクト参照のsetOnClickListener(android.view.View $ OnClickListener)'
実験試験2使用getSelectedItem()
方法は、位置、トーストに示す最初の位置の値の結果を取得します。ここで
は
Javaコードの実装
private void DialogTest(){
String[] Title = {"Select Title","Teacher","Employer","Lecturer","Coach","Mentor","Co-Worker","Prinicpal","Dean","Other"};
LayoutInflater layoutInflater =(LayoutInflater)getActivity().getBaseContext().getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.dialog_test, null);
final PopupWindow popupWindow = new PopupWindow(popupView, isPortrait ? getHeightByPercentage(50) : getWidthByPercentage(50), LinearLayout.LayoutParams.WRAP_CONTENT);
Spinner popupSpinner = (Spinner)popupView.findViewById(R.id.popupspinner);
Button button_test_cancel =(Button)popupView.findViewById(R.id.button_test_cancel);
Button button_test_send =(Button)popupView.findViewById(R.id.button_test_send);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, Title);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
popupSpinner.setAdapter(adapter);
popupWindow.showAsDropDown(btn_request_testimonial, 50, -30);
//Step-1 Experimental Test
popupSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int selectedPosition = position;
Toast.makeText(getActivity().getBaseContext(),selectedPosition, Toast.LENGTH_LONG).show();
}
});
//Step-2 Experimental Test
final String categoryTitle = popupSpinner.getSelectedItem().toString();
Toast.makeText(getActivity().getBaseContext(),categoryTitle, Toast.LENGTH_LONG).show();
button_test_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
button_test_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
}
dialog_test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:background="@color/white"
android:orientation="vertical">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="38dp"
android:background="@color/colorAccent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/testimonial_request"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="@color/white"
android:layout_centerInParent="true"/>
</RelativeLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp">
<EditText
android:id="@+id/edTxt_Testi_Name"
style="@style/edittextstyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:inputType="textPersonName"
android:textColor="@color/colorPrimaryDark"
android:textColorHint="@color/colorAccent"
tools:ignore="HardcodedText"/>
<!--android:text="[email protected]"-->
</android.support.design.widget.TextInputLayout>
<Spinner
android:id="@+id/popupspinner"
style="@style/edittextstyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
android:textColor="@color/colorPrimaryDark"
android:textColorHint="@color/colorAccent"/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp">
<EditText
android:id="@+id/edTxt_Testi_Email"
style="@style/edittextstyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="E-Mail"
android:inputType="textEmailAddress"
android:textColor="@color/colorPrimaryDark"
android:textColorHint="@color/colorAccent"
tools:ignore="HardcodedText"/>
<!--android:text="[email protected]"-->
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp">
<EditText
android:id="@+id/edTxt_Testi_Org"
style="@style/edittextstyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Organization Name"
android:inputType="textPersonName"
android:textColor="@color/colorPrimaryDark"
android:textColorHint="@color/colorAccent"
tools:ignore="HardcodedText"/>
<!--android:text="[email protected]"-->
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp">
<EditText
android:id="@+id/edTxt_Testi_Comments"
style="@style/edittextstyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Comments"
android:inputType="textMultiLine"
android:textColor="@color/colorPrimaryDark"
android:textColorHint="@color/colorAccent"
tools:ignore="HardcodedText"/>
<!--android:text="[email protected]"-->
</android.support.design.widget.TextInputLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button_test_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" android:layout_alignParentTop="true"
android:background="@color/colorAccent"
android:text="Send"
android:textColor="@color/white"
android:textSize="12sp"/>
<Button
android:id="@+id/button_test_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button_test_send"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:layout_toLeftOf="@+id/button_test_send" android:layout_toStartOf="@+id/button_test_send"
android:background="@color/colorAccent"
android:text="Cancel"
android:textColor="@color/white"
android:textSize="12sp"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
ありがとうございました。 –