根据生肖选择年份控件

来源:互联网 发布:js sort函数 编辑:程序博客网 时间:2024/03/29 06:47

package com.**.mobile.common;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.graphics.Color;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TableLayout;import android.widget.TableRow;import android.widget.TextView;public class ShengXiao {private Context context = null;private TextView current = null;private Button helpbutton = null;private EditText yeartext = null;public ShengXiao(Context context, Button helpbutton, EditText yeartext) {this.context = context;this.helpbutton = helpbutton;this.yeartext = yeartext;}public void append() {helpbutton.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {current = new TextView(context);String[] items = new String[] {"子鼠: 2008 1996 1984 1972 1960 1948 1936 1924 1912","丑牛: 2009 1997 1985 1973 1961 1949 1937 1925 1913","寅虎: 2010 1998 1986 1974 1962 1950 1938 1926 1914","卯兔: 2011 1999 1987 1975 1963 1951 1939 1927 1915","辰龙: 2012 2000 1988 1976 1964 1952 1940 1928 1916","巳蛇: 2013 2001 1989 1977 1965 1953 1941 1929 1917","午马: 2002 1990 1978 1966 1954 1942 1930 1918","未羊: 2003 1991 1979 1967 1955 1943 1931 1919","申猴: 2004 1992 1980 1968 1956 1944 1932 1920","酉鸡: 2005 1993 1981 1969 1957 1945 1933 1921","戌狗: 2006 1994 1982 1970 1958 1946 1934 1922","亥猪: 2007 1995 1983 1971 1959 1947 1935 1923 1911", };TableLayout table = new TableLayout(context);for (int i = 0; i < items.length; i++) {TableRow row = new TableRow(context);if (i % 2 == 1) {row.setBackgroundColor(Color.GRAY);}String[] years = items[i].split(" ");for (int j = 0; j < years.length; j++) {final TextView text = new TextView(context);text.setText(years[j] + " ");text.setTextSize(20);text.setTextColor(Color.WHITE);if (j != 0) {text.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {current.setTextColor(Color.WHITE);text.setTextColor(Color.argb(255, 75, 204,255));String year = ((String) text.getText()).substring(0, 4);text.setText(year);current = text;}});}row.addView(text);}table.addView(row);}AlertDialog.Builder builder = new AlertDialog.Builder(context);builder.setTitle("根据生肖查询出生年份").setView(table).setPositiveButton("确定",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {String currentText = current.getText().toString();if(!"".equals(currentText)&& currentText!=null)yeartext.setText(currentText);}}).setNegativeButton("取消", null).create().show();}});}}

点击查询按钮,弹出生肖控件,选择年份按确定,年份添加到文本框中