package loujin.android;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
public class AndroidprojectActivity extends Activity {
private static final String[] m_Countries = { "信息系", "贸经系", "机电系", "旅游系", "轻纺系","工商系" };
private static final String[] autoString={"TextView","ListView","Tost","EditText","RadioGroup",
"RadioButton","CheckBox","Spinner","AutoCompleteTextView","Image","ImageButton"};
private TextView m_TextView, k_TextView,d_TextView,t_TextView;
private Spinner m_Spinner;
private ArrayAdapter<String> adapter;
Button m_dpButton;
Button m_tpButton;
Calendar c;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MultiAutoCompleteTextView mm_AutoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.MultiAutoCompleteTextView01);
//将adapter添加到AutoCompleteTextView中
mm_AutoCompleteTextView.setAdapter(adapter);
mm_AutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
m_TextView = (TextView) findViewById(R.id.TextView1);
m_Spinner = (Spinner) findViewById(R.id.Spinner1);
k_TextView=(TextView)findViewById(R.id.TextView2);
//时间
c=Calendar.getInstance();
d_TextView=(TextView)findViewById(R.id.TextView3);
t_TextView=(TextView)findViewById(R.id.TextView4);
m_dpButton = (Button)findViewById(R.id.button1);
m_tpButton = (Button)findViewById(R.id.button2);
//将可选内容与ArrayAdapter连接
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m_Countries);
//设置下拉列表的风格
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//将adapter添加到m_Spinner中
m_Spinner.setAdapter(adapter);
//添加事件监听
m_Spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
m_TextView.setText("你所在的系为:" + m_Countries[arg2]);
//设置显示当前选择的项
arg0.setVisibility(View.VISIBLE);
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
//提示框
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, autoString);
MultiAutoCompleteTextView mm_AutoCompleteTextView1 = (MultiAutoCompleteTextView) findViewById(R.id.MultiAutoCompleteTextView01);
//将adapter添加到AutoCompleteTextView中
mm_AutoCompleteTextView1.setAdapter(adapter);
mm_AutoCompleteTextView1.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
//设置时间
m_dpButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
new DatePickerDialog(AndroidprojectActivity.this,
new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
//设置日历
t_TextView.setText("你设置的日期为:"+ year+"年"+ (monthOfYear+1)+"月"+dayOfMonth+"日");
//设置显示当前选择的项
view.setVisibility(View.VISIBLE);
}
},c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show();
}
});
m_tpButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
new TimePickerDialog(AndroidprojectActivity.this,
new TimePickerDialog.OnTimeSetListener()
{
public void onTimeSet(TimePicker view, int hourOfDay,int minute)
{
//设置时间
d_TextView.setText("你设置的时间为:" + hourOfDay+"点"+minute+"分");
//设置显示当前选择的项
view.setVisibility(View.VISIBLE);
}
},c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true).show();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:filterTouchesWhenObscured="true"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="77dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="日期" />
<TextView
android:id="@+id/TextView4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_marginBottom="14dp"
android:text="@string/TextView4" />
<MultiAutoCompleteTextView
android:id="@+id/MultiAutoCompleteTextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/TextView4"
android:layout_alignParentLeft="true"
android:layout_marginBottom="16dp" />
<TextView
android:id="@+id/TextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/hello" />
<TextView
android:id="@+id/TextView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/MultiAutoCompleteTextView01"
android:layout_alignParentLeft="true"
android:text="@string/TextView2" />
<Spinner
android:id="@+id/Spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/TextView1"
android:layout_marginTop="14dp" />
<Button
android:id="@+id/button2"
android:layout_width="77dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginTop="64dp"
android:text="时间" />
<TextView
android:id="@+id/TextView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginTop="30dp"
android:text="@string/TextView3" />
</RelativeLayout>