android中的edittext spinner

时间:2014-05-06 10:18:44

标签: android android-spinner

您好我的应用程序我正在设计一个弹出式注册表单。在那个类字段中,我想显示那个我使用微调器的下拉菜单,但是那个它采用单独的字段
我不想在单独的领域我想要代替课我想要下拉菜单可以请告诉我我做错了什么。

popupregistration.java:

    public class PopupRegistration extends Activity implements OnItemSelectedListener 
   {
    private String[] state = { "1", "2", "3",
            "4", "5", "6", "7",
            "8", "9"};

    public String log;
    public DatabaseHandlers db;

    GSfeedback cn;

    EditText etName,etClass,etrollno,etparentname,etphno; 
    Button btnregister;

    Spinner spinnerOsversions;
    EditText selVersion;
    TextView textView;
    String PHONE_REGEX;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popup_registration);
        System.out.println(state.length);
        db = new DatabaseHandlers(this);

        etName=(EditText)findViewById(R.id.etName); 
        selVersion=(EditText)findViewById(R.id.selVersion);
        etrollno=(EditText)findViewById(R.id.etrollno);
        etparentname=(EditText)findViewById(R.id.etparentname);
        etphno=(EditText)findViewById(R.id.etphno);

        btnregister=(Button)findViewById(R.id.registerbutton);

        PHONE_REGEX = "[0-9]+";
        spinnerOsversions = (Spinner) findViewById(R.id.osversions);
        ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, state);
        adapter_state
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerOsversions.setAdapter(adapter_state);
        spinnerOsversions.setOnItemSelectedListener(this);


        btnregister.setOnClickListener(new View.OnClickListener() 
        {
        public void onClick(View v) {
             if(!validate()) {
                //Toast.makeText(getApplicationContext(), "Not Valid", Toast.LENGTH_SHORT).show();
             } else {
             if(!validates()) {
                    // Toast.makeText(getApplicationContext(), "Not valid", Toast.LENGTH_SHORT).show(); 
                 } else {
                     //Toast.makeText(getApplicationContext(), "valid", Toast.LENGTH_SHORT).show();
                     String studentname=etName.getText().toString(); 
                     String classid=etClass.getText().toString();
                     String rollno=etrollno.getText().toString(); 
                    String parentname=etparentname.getText().toString();
                     String phno=etphno.getText().toString();

                     // Save the Data in Database
                     db.addGSfeedback(new GSfeedback(studentname,classid,rollno,parentname,phno));

                     Toast.makeText(getApplicationContext(), "Thanks for Registration, Welcome to Lilttle Flowers Public School Android App.", Toast.LENGTH_LONG).show();

                     etName.setText("");
                     etClass.setText("");
                     etrollno.setText("");
                     etparentname.setText("");
                     etphno.setText("");

                     finish();
                     Intent nextScreen = new Intent(getApplicationContext(), Splashscreen.class);
                     startActivity(nextScreen);

                 }
             }
        }

        });
        }


        public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
            spinnerOsversions.setSelection(position);
            String selState = (String) spinnerOsversions.getSelectedItem();
            selVersion.setText("Selected Android OS:" + selState);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }




        private boolean validate(){
        if(etName.length() == 0 || etClass.length() == 0 || etrollno.length() == 0 || etphno.length() == 0 || etparentname.length() == 0){
            Toast.makeText(getApplicationContext(), "pls fill the empty fields", Toast.LENGTH_SHORT).show();
            return false;
        } if(etName.length() > 30 && etparentname.length() > 30){
            //Toast.makeText(getApplicationContext(), "pls enter less the 25 characher", Toast.LENGTH_SHORT).show();
            etName.setError("pls enter less the 30 charachter");                
            return true;

        } else if(etphno.length() < 6 || etphno.length() > 13){
            //etNumber.setError("Not Valid Phone Number");
            Toast.makeText(getApplicationContext(), "Enter valid Phone Number", Toast.LENGTH_SHORT).show();
            return false;
        } else {
            return true;
        }
    }

        private boolean validates(){
        if(etphno.getText().toString().trim().matches(PHONE_REGEX)) {
            return true;
        } else {     
            Toast.makeText(getApplicationContext(), "Enter Valid Phone Number", Toast.LENGTH_SHORT).show();   
             return false;              
        }
    }
    }

popupregistration.xml:

<?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"
    android:gravity="center_vertical" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >       

    <EditText
        android:id="@+id/etName"
        android:hint="Student Name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"> 
        <requestFocus />       
    </EditText>

     <Spinner
        android:id="@+id/osversions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />
    <EditText
        android:id="@+id/selVersion" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
          android:layout_below="@+id/osversions"
        android:hint="Class"/>
    <EditText
        android:id="@+id/etrollno"
        android:hint="Roll Number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"> 
        <requestFocus />       
    </EditText>

     <EditText
        android:id="@+id/etparentname"
        android:hint="Parent Name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"> 
        <requestFocus />       
    </EditText>
    <EditText
        android:id="@+id/etphno" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Phone Number" />

    <Button
        android:id="@+id/registerbutton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="REGISTER"
        android:layout_marginBottom="60dp" />

    </LinearLayout>
    </ScrollView>

   </LinearLayout>

谢谢。

1 个答案:

答案 0 :(得分:0)

如果您想要注册弹出窗口,可以使用PopupWindow而不是Spinner。您可以使用微调器从列表中选择一个值,而不是显示注册弹出窗口。

并且PopupWindow有一个方法showAsDropDown,它会在锚点下方显示它,使其看起来像一个下拉列表。

希望这有帮助

示例:

    // inflate your layout where you might add some edit texts for login and password
    View popupWindowView = getActivity().getLayoutInflater().inflate(R.layout.your_layout, null, false);
    mPw = new PopupWindow(
            popupWindowView,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            true);

    mPw.setOutsideTouchable(true);
    mPw.showAsDropDown(anchor, xOffset, yOffset);