PopUp window 微调器 OnItem 选择不工作
PopUp window with Spinner OnItem Selection Not Working
我试图在弹出窗口中实现 spinner
,选择带有 OnItemselection
的 spinner
项目不起作用,在这种情况下我有两种实验测试。
实验测试-1我已经实现了OnItemSelectionListener
,实验测试-1的结果是
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference.
Experimental Test-2使用getSelectedItem()
方法获取位置,Toast中只显示第一个位置值的结果。
这是我的代码,
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="Trd@sm20"-->
</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="Trd@sm20"-->
</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="Trd@sm20"-->
</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="Trd@sm20"-->
</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>
试试这个
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Object item = arg0.getItemAtPosition(arg2);
if (item!=null) {
Toast.makeText(MainActivity.this, item.toString(),
Toast.LENGTH_SHORT).show();
}
Toast.makeText(MainActivity.this, "Selected",
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
您正在将 onClickListener
设置为 spinner
因此它被第一个项目点击。评分者你应该使用 onItemSelectedListener
//Step-1 Experimental Test
popupSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(SolveTest.this, String.valueOf(position), Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
我试图在弹出窗口中实现 spinner
,选择带有 OnItemselection
的 spinner
项目不起作用,在这种情况下我有两种实验测试。
实验测试-1我已经实现了OnItemSelectionListener
,实验测试-1的结果是
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference.
Experimental Test-2使用getSelectedItem()
方法获取位置,Toast中只显示第一个位置值的结果。
这是我的代码,
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="Trd@sm20"-->
</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="Trd@sm20"-->
</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="Trd@sm20"-->
</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="Trd@sm20"-->
</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>
试试这个
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Object item = arg0.getItemAtPosition(arg2);
if (item!=null) {
Toast.makeText(MainActivity.this, item.toString(),
Toast.LENGTH_SHORT).show();
}
Toast.makeText(MainActivity.this, "Selected",
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
您正在将 onClickListener
设置为 spinner
因此它被第一个项目点击。评分者你应该使用 onItemSelectedListener
//Step-1 Experimental Test
popupSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(SolveTest.this, String.valueOf(position), Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});