在我的应用程序中,我在代码中动态创建微调器。我想强制用户单击微调器并更改其值/内容。否则,用户将无法通过单击“下一步”按钮进入下一个屏幕。
如何在 Android 中做到这一点?有人有什么想法吗?
提前致谢。
罗汉
你可以使用这个:
if (spin.getSelectedItemPosition() < 0) {//Do something}
这意味着用户没有选择任何东西。
让 Spinner 的第一个值类似于“-please select-”。
当用户单击下一个按钮时,执行验证并检查微调器中 selectedItem 的值是否为“-please select-”,如果是,则显示吐司并要求用户从微调器中选择一些东西。
你需要代码,让我知道。
使用此代码检查微调器项目是否被选中。
两个标志都在类级别(全局)。
Boolean temp = false;
Boolean check = false;
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if(temp){
check = true;
}
temp = true;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
check = false;
}
});
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(check){
//perform when user select spinner item
}else{
//put Dialog for alert please select spinner item
}
}
}
创建一个布尔全局变量,如..
boolean isSelect = false;
现在,当用户从微调器中选择值时,然后将其设为isSelect = false
。当用户单击 NEXT 按钮时,检查条件isSelect
是真还是假。
而已。
在那个微调器中调用你Intent
的下一个Activity
Click Listener
您可以使用以下方法获取所选项目的值。
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if (spinner.getSelectedItem().toString().equals("YourValue")) {
Intent yourIntent = new Intent(this,YourClassName.class);
startActivity(yourIntent);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
就我而言,我在列表的第一个位置添加了一个额外的项目
1.“选择一些东西” 2.“下一个” 3.“上一个” 5.... 6..
然后在使用
String item=spinnerObject.getSelectedItem ();
now check if("Select Something".equels(item)){
show some dialog to select anything from spinner
}else{
send it to next screen
}
我制作了一个新的 Spinner 类,封装了上述原则。但即使那样你也必须确保调用正确的方法而不是setSelection
要点相同
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
/**
* Used this to differentiate between user selected and prorammatically selected
* Call {@link Spinner#programmaticallySetPosition} to use this feature.
* Created by vedant on 6/1/15.
*/
public class Spinner extends android.widget.Spinner implements AdapterView.OnItemSelectedListener {
OnItemSelectedListener mListener;
/**
* used to ascertain whether the user selected an item on spinner (and not programmatically)
*/
private boolean mUserActionOnSpinner = true;
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (mListener != null) {
mListener.onItemSelected(parent, view, position, id, mUserActionOnSpinner);
}
// reset variable, so that it will always be true unless tampered with
mUserActionOnSpinner = true;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
if (mListener != null)
mListener.onNothingSelected(parent);
}
public interface OnItemSelectedListener {
/**
* <p>Callback method to be invoked when an item in this view has been
* selected. This callback is invoked only when the newly selected
* position is different from the previously selected position or if
* there was no selected item.</p>
*
* Impelmenters can call getItemAtPosition(position) if they need to access the
* data associated with the selected item.
*
* @param parent The AdapterView where the selection happened
* @param view The view within the AdapterView that was clicked
* @param position The position of the view in the adapter
* @param id The row id of the item that is selected
*/
void onItemSelected(AdapterView<?> parent, View view, int position, long id, boolean userSelected);
/**
* Callback method to be invoked when the selection disappears from this
* view. The selection can disappear for instance when touch is activated
* or when the adapter becomes empty.
*
* @param parent The AdapterView that now contains no selected item.
*/
void onNothingSelected(AdapterView<?> parent);
}
public void programmaticallySetPosition(int pos, boolean animate) {
mUserActionOnSpinner = false;
setSelection(pos, animate);
}
public void setOnItemSelectedListener (OnItemSelectedListener listener) {
mListener = listener;
}
public Spinner(Context context) {
super(context);
super.setOnItemSelectedListener(this);
}
public Spinner(Context context, int mode) {
super(context, mode);
super.setOnItemSelectedListener(this);
}
public Spinner(Context context, AttributeSet attrs) {
super(context, attrs);
super.setOnItemSelectedListener(this);
}
public Spinner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
super.setOnItemSelectedListener(this);
}
public Spinner(Context context, AttributeSet attrs, int defStyle, int mode) {
super(context, attrs, defStyle, mode);
super.setOnItemSelectedListener(this);
}
}