我想在我的机器人中弹出一个弹出窗口

时间:2016-04-21 08:09:55

标签: android android-popupwindow

I want a pop like this when i click an item in the list. In the popupmenu i should not be able to increase my height above 7 ft and 0 inch and shouldnt decrease my height below 3 ft and 0 inch.

这是我的活动

package com.example.shivnandan.fit;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity1 extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener


{

    ListView listView;

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


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Home");


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        /////////////////////////////////////////////////////////

        listView = (ListView) findViewById(R.id.listView2);

        final ListViewItem[] items = new ListViewItem[5];

        for (int i = 0; i < items.length; i++)
        {
            if (i == 0)

            {
                items[i] = new ListViewItem("White " + i, CustomAdapter.TYPE_WHITE); //mainactivity
            }

            else if (i == 1)

            {
                items[i] = new ListViewItem( "Black " + i, CustomAdapter.TYPE_BLACK ); //1
            }

            else if (i == 2)

            {
                items[i] = new ListViewItem("EVEN " + i, CustomAdapter.TYPE_ODD); //2
            }

            else if (i == 3)

            {
                items[i] = new ListViewItem("EVEN " + i, CustomAdapter.TYPE_EVEN); //3
            }

            else

            {
                items[i] = new ListViewItem("EVEN " + i, CustomAdapter.TYPE_FOUR); //4
            }

           /* else

            {
                items[i] = new ListViewItem( "odd"+i,CustomAdapter.TYPE_WHITE);
            }*/
        }

        CustomAdapter customAdapter = new CustomAdapter(this, R.id.text, items);
        listView.setAdapter(customAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l)
            {
                if (i == 0)
                {

                    openDialogManager();

                }


            }
        });
    }
    public void openDialogManager()
    {
        final TextView tv3 = (TextView)findViewById(R.id.hometext);
        final CharSequence[] items = {"Male", "Female"};

        new AlertDialog.Builder(MainActivity1.this)
                .setTitle("Gender")
                .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        switch (which)
                        {

                            case 0:
                               // Toast.makeText( MainActivity1.this, "male ", Toast.LENGTH_SHORT).show();
                                tv3.setText("Male");
                                dialog.dismiss();
                                break;

                            case 1:
                               // Toast.makeText(MainActivity1.this, "female ", Toast.LENGTH_SHORT).show();
                                tv3.setText("Female");
                                dialog.dismiss();
                                break;
                        }
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        dialog.dismiss();
                    }
                })
        .show();
    }
    public void openDialogManagerr()
    {
        final TextView tv3 = (TextView)findViewById(R.id.hometext);
        final CharSequence[] items = {"Male", "Female"};

        new AlertDialog.Builder(MainActivity1.this)
                .setTitle("Gender")
                .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        switch (which)
                        {

                            case 0:
                                // Toast.makeText( MainActivity1.this, "male ", Toast.LENGTH_SHORT).show();
                                tv3.setText("Male");
                                dialog.dismiss();
                                break;

                            case 1:
                                // Toast.makeText(MainActivity1.this, "female ", Toast.LENGTH_SHORT).show();
                                tv3.setText("Female");
                                dialog.dismiss();
                                break;
                        }
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        dialog.dismiss();
                    }
                })
                .show();
    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
    {
        super.onCreateContextMenu(menu, v, menuInfo);

    }

    @Override
    public void onBackPressed()
    {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START))
        {

            drawer.closeDrawer(GravityCompat.START);
        }
        else
        {

            super.onBackPressed();
        }



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            Toast.makeText(getApplicationContext(), " Setting ", Toast.LENGTH_SHORT).show();
            return true;
        }

        if(id==R.id.Change_photo)
        {
            Toast.makeText(getApplicationContext(),"Change Your Photo",Toast.LENGTH_SHORT).show();


        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item)
    {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera)
        {

        }
        else if (id == R.id.nav_gallery)
        {
            Intent i = new Intent(getApplicationContext(),MainActivity.class);

            startActivity(i);
        }
        else if (id == R.id.nav_slideshow)
        {


        }
    else if (id == R.id.nav_manage)
     {

       }


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

}


class ListViewItem
{
    private String text;
    private int type;

    public ListViewItem(String text, int type)
    {
        this.text = text;
        this.type = type;
    }



    public String getText()
    {
        return text;
    }

    public void setText(String text)
    {
        this.text = text;
    }

    public int getType()
    {
        return type;
    }

    public void setType(int type)
    {
        this.type = type;
    }

}


class ViewHolder
{
    TextView text;

    public ViewHolder(TextView text) {
        this.text = text;
    }

    public TextView getText() {
        return text;
    }

    public void setText(TextView text) {
        this.text = text;
    }

}
class CustomAdapter extends ArrayAdapter
{

    public static final int TYPE_ODD = 0;
    public static final int TYPE_EVEN = 1;
    public static final int TYPE_WHITE = 2;
    public static final int TYPE_BLACK = 3;
    public static final int TYPE_FOUR = 4;
    private ListViewItem[] objects;

    @Override
    public int getViewTypeCount()
    {
        return 4;
    }

    @Override
    public int getItemViewType(int position)
    {
        return objects[position].getType();
    }

    public CustomAdapter(MainActivity1 context, int resource, ListViewItem[] objects)
    {
        super(context, resource, objects);
        this.objects = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {

        ViewHolder viewHolder = null;
        ListViewItem listViewItem = objects[position];
        int listViewItemType = getItemViewType(position);


        if (convertView == null)
        {

            if (listViewItemType == TYPE_EVEN)
            {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.even, null);
            }
            else if (listViewItemType == TYPE_ODD)
            {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.odd, null);
            }
            else if (listViewItemType == TYPE_WHITE)
            {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.white, null);
            }
            else if (listViewItemType == TYPE_FOUR)
            {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.four, null);
            }
            else
            {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.black, null);
            }

            TextView textView = (TextView) convertView.findViewById(R.id.text);
            viewHolder = new ViewHolder(textView);

            convertView.setTag(viewHolder);

        }


        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // viewHolder.getText().setText(listViewItem.getText());

        return convertView;
    }

}

1 个答案:

答案 0 :(得分:0)

1.创建自定义对话框并根据您的需求进行调整,例如: G。 my_dialog.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


    <TextView
        android:id="@+id/tv_dialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text"/>

    <Button
        android:id="@+id/btn_dialog"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@+id/tv_dialog"
        android:text="Button"/>

</RelativeLayout>

2.Declare并从活动中打开对话框:

        my_dialog = new Dialog(context);
        my_dialog .requestWindowFeature(Window.FEATURE_NO_TITLE);
        my_dialog .setCancelable(true);
        my_dialog .setContentView(R.layout.my_dialog);
        my_dialog.show();