note-31 Show dialog , DatePicker , AutoCompleteTextView

A.  show dialog:

   1.    set a dialog id .

   2.    context.showDialog(int dialogId);

   3.    override onCreateDialog(int dialogId) function , use switch case to filter the Ids to return the new  dialog ;

B.   DatePicker

      1. no need to set layout .

      2.  set a   DatePickerDialog.onDateSetListener , override the onDateSet function ; Indicate the action  after press "set" button

      3.   new it directly  :  new DatePickerDialog(this,onDateSetListener,2012,11-1,30) ;

C.   AutoCompleteTextView

      1.    set a list to provide data as the same as Spinner.

      2.    set a adapter layout as the Spinner , MUST HAVE a TextView in the layout

      3.    new an ArrayAdapter with for params   as the Spinner

              new ArrayAdapter(Context context , int layout , int textViewId , List  list );

So the ArrayAdapter will find the textView by the textViewId and insert the String on it.

 

package com.datapicker_test;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.DatePicker;
import java.util.ArrayList;
import java.util.List;

public class DatePickerTestActivity extends Activity
{
    
    private Button showDatePickerButton;
    private static final int DATE_PICKER_ID=1;
    
    private AutoCompleteTextView autoText;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        showDatePickerButton=(Button)this.findViewById(R.id.showDataPickerItem);
        showDatePickerButton.setOnClickListener(showListener);
        
        
        
        autoText=(AutoCompleteTextView)this.findViewById(R.id.autoComplete);
        
        List<String> list=new ArrayList<String>();
        list.add("something1");
        list.add("something2");
        
        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(this,
                R.layout.list_item,
                R.id.autoText,
                list);
        
        autoText.setAdapter(arrayAdapter);
    }
    
    
    private OnClickListener showListener=new OnClickListener(){

        public void onClick(View arg0) {
//            throw new UnsupportedOperationException("Not supported yet.");
            DatePickerTestActivity.this.showDialog(DATE_PICKER_ID);
            
        }
        
    };

    @Override
    protected Dialog onCreateDialog(int id) {
        
        switch(id){
            case DATE_PICKER_ID:
                return new DatePickerDialog(this,onDateSetListener,2012,11-1,30);
        }
        return super.onCreateDialog(id);
    }
    
    private DatePickerDialog.OnDateSetListener onDateSetListener=
            new DatePickerDialog.OnDateSetListener(){

        public void onDateSet(DatePicker datePicker, int y, int m, int d) {
//            throw new UnsupportedOperationException("Not supported yet.");
            Log.i("l","year "+y+"  month "+(m+1)+"   day "+d);
        }
        
    };
    
    
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Hello World, DataPickerTestActivity"
    />
    
<Button
    android:id="@+id/showDataPickerItem"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="show date picker"
    />
    
<AutoCompleteTextView
    android:id="@+id/autoComplete"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
        
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/ic_dialog_alert"
    />
<TextView  
    android:id="@+id/autoText"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Hello World, DataPickerTestActivity"
    android:textColor="#000000"
    />
</LinearLayout>

猜你喜欢

转载自julianjulian8.iteye.com/blog/1739138