自定义Spinner---利用TextView以及Dialog组合

MainActivity:



package com.example.myspinner_bytextview;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author 鹭岛猥琐男
 *
 */
public class MainActivity extends Activity
{

    private TextView tv_show;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View sex = findViewById(R.id.sex);
        tv_show = (TextView) findViewById(R.id.tv_show);
        sex.setOnClickListener(new OnClickListener()
        {
           
            @Override
            public void onClick(View v)
            {
                AlertDialog.Builder builder;
                final AlertDialog dialog;
                View view = getLayoutInflater().inflate(R.layout.myspinner, null);
                builder=new AlertDialog.Builder(MainActivity.this);
                dialog=builder.create();
                dialog.setView(view, 0, 0, 0, 0);
                dialog.show();
                View sex_man = view.findViewById(R.id.sex_man);
                View woman = view.findViewById(R.id.woman);
                View shemale = view.findViewById(R.id.shemale);
                sex_man.setOnClickListener(new OnClickListener()
                {
                   
                    @Override
                    public void onClick(View v)
                    {
                        tv_show.setText("男性");
                        Toast.makeText(MainActivity.this, "性别:男性", Toast.LENGTH_LONG).show();
                        dialog.dismiss();
                       
                    }
                });
               
                woman.setOnClickListener(new OnClickListener()
                {
                   
                    @Override
                    public void onClick(View v)
                    {
                        tv_show.setText("女性");
                        Toast.makeText(MainActivity.this, "性别:男性", Toast.LENGTH_LONG).show();
                        dialog.dismiss();
                       
                    }
                });
               
                shemale.setOnClickListener(new OnClickListener()
                {
                   
                    @Override
                    public void onClick(View v)
                    {
                        tv_show.setText("人妖");
                        Toast.makeText(MainActivity.this, "性别:男性", Toast.LENGTH_LONG).show();
                        dialog.dismiss();
                       
                    }
                });
               
            }
        });
    }

    @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;
    }

}



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=".MainActivity" >

    <RelativeLayout
        android:id="@+id/sex"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/sex_selector"
        android:layout_marginTop="60dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="30dp"
            android:text="性别"
            android:textSize="20sp"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/tv_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:text="男性"
             android:textSize="20sp"
             android:textColor="#646464"
            android:layout_toLeftOf="@+id/imageView1"
            android:layout_centerVertical="true"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="30dp"
            android:contentDescription="@string/app_name"
            android:src="@drawable/mydown" />

    </RelativeLayout>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/sex"
        android:background="#dcdcdc"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/sex"
        android:layout_centerHorizontal="true"
        android:background="#dcdcdc"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>



mydialog.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="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="#dcdcdc" />

    <RelativeLayout
        android:id="@+id/sex_man"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_below="@+id/textView1"
         android:background="@drawable/sex_selector"
        android:layout_centerHorizontal="true" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="男性"
            android:textSize="20sp"
          
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </RelativeLayout>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/sex_man"
        android:background="#dcdcdc"
        android:layout_centerHorizontal="true"
         />

    <RelativeLayout
        android:id="@+id/woman"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_below="@+id/textView3"
         android:background="@drawable/sex_selector"
        android:layout_centerHorizontal="true" >

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="女性"
            android:textSize="20sp"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </RelativeLayout>

    <TextView
        android:id="@+id/textView5"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/woman"
        android:layout_centerHorizontal="true"
        android:background="#dcdcdc"
         />

    <RelativeLayout
        android:id="@+id/shemale"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_below="@+id/textView5"
        android:background="@drawable/sex_selector"
        android:layout_centerHorizontal="true" >

        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="人妖"
            android:textSize="20sp"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </RelativeLayout>

    <TextView
        android:id="@+id/textView7"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#dcdcdc"
        android:layout_below="@+id/shemale"
        android:layout_centerHorizontal="true"
         />

</RelativeLayout>



wKioL1OlHb6g3ysjAACgq0CLghw010.jpg

wKioL1OlHb-RrcgMAACUCBkTAr8436.jpg

wKiom1OlHe2D0ZG5AACg5iyXCjM018.jpg


DownLoad:http://download.csdn.net/detail/zzw0221/7530723


你可能感兴趣的:(Spinner,textview,dialog)