使用微调器和edittext字段创建自定义alertdialog

时间:2016-03-15 23:55:30

标签: android android-spinner android-alertdialog

我试图在按下按钮时弹出一个警告对话框,并向用户显示一些要填写的字段,并在下拉菜单中选择正确的选项。

这是我当前的代码,但我觉得我需要创建一个自定义适配器而不是使用基本适配器,但我不确定如何创建一个。感谢您的帮助。

public void addWork() {

    weightList = new ArrayList<String>();

    weightRes.moveToFirst();
    for (int i = 0; i < weightRes.getCount(); i++) {
        weightList.add(weightRes.getString(1));
    }

    Context context = ClassDescription.this;
    final LinearLayout layout = new LinearLayout(context);
    layout.setOrientation(LinearLayout.VERTICAL);

    final EditText titleBox = new EditText(context);
    titleBox.setHint("Title");
    layout.addView(titleBox);

    final EditText pointsEarned = new EditText(context);
    pointsEarned.setHint("Points Earned");
    layout.addView(pointsEarned);

    final EditText maxPoints = new EditText(context);
    maxPoints.setHint("Total Possible Points");
    layout.addView(maxPoints);

    final Spinner sp = new Spinner(ClassDescription.this);
    sp.setLayoutParams(new LinearLayout.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT));
    sp.setAdapter(adp);
    layout.addView(sp);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.work_dialog, weightList);
    sp.setAdapter(adapter);

    AlertDialog.Builder builder = new AlertDialog.Builder(ClassDescription.this);
    builder.setTitle("Add Work for "+res.getString(1));
    //builder.setAdapter(adapter, null);

    // Set up the input
    // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
    builder.setView(layout);


    // Set up the buttons
    //TODO make addData add the inputed information into the list. Add information to database when done button is pressed
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {


        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    builder.show();
}

1 个答案:

答案 0 :(得分:4)

这是我的代码并且很容易上手:

<强> MainActivity.java

package com.example.example1;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    Button btn_popup;

    String[] title;
    String spinner_item;

    SpinnerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        title = getResources().getStringArray(R.array.titles);

        btn_popup = (Button) findViewById(R.id.button1);

        adapter=new SpinnerAdapter(getApplicationContext());

        btn_popup.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final Dialog dialog = new Dialog(MainActivity.this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.row_spinner);
                dialog.setCancelable(true);

                // set the custom dialog components - text, image and button
                final Spinner spinner = (Spinner) dialog.findViewById(R.id.spinner1);
                final EditText edittext = (EditText) dialog.findViewById(R.id.editText1);
                Button button = (Button) dialog.findViewById(R.id.button1);
                spinner.setAdapter(adapter);
                spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        // TODO Auto-generated method stub
                        spinner_item = title[position];
                    }

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

                    }
                });

                button.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                        Toast.makeText(getApplicationContext(), spinner_item + " - " + edittext.getText().toString().trim(), Toast.LENGTH_LONG).show();
                    }
                });
                dialog.show();

            }
        });
    }

    public class SpinnerAdapter extends BaseAdapter {
        Context context;
        private LayoutInflater mInflater;

        public SpinnerAdapter(Context context) {
            this.context = context;
        }

        @Override
        public int getCount() {
            return title.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ListContent holder;
            View v = convertView;
            if (v == null) {
                mInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
                v = mInflater.inflate(R.layout.row_textview, null);
                holder = new ListContent();
                holder.text = (TextView) v.findViewById(R.id.textView1);

                v.setTag(holder);
            } else {
                holder = (ListContent) v.getTag();
            }

            holder.text.setText(title[position]);

            return v;
        }
    }

    static class ListContent {
        TextView text;
    }
}

<强> activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Popup Button" />

</RelativeLayout>

<强> row_spinner.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spinner1"
        android:layout_centerHorizontal="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:text="Okay" />

</RelativeLayout>

<强> row_textview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textColor="@android:color/black"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

<强>的strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">example1</string>
    <string name="hello_world">Hello world!</string>

    <string-array name="titles">
        <item>Prakash</item>
        <item>Bhavin</item>
        <item>Chirag</item>
        <item>Yakub</item>
    </string-array>

</resources>