2011-08-25 15 views
7

Desidero visualizzare una finestra di avviso dopo aver fatto clic su un pulsante per visualizzare una barra di ricerca in modo che la persona possa modificare il valore da 1 a 48. Ho creato un xml personalizzato che ha una barra di ricerca e due viste di testo e vorrei che la finestra di dialogo avesse due pulsanti, uno solo annulla la finestra di dialogo e l'altro fa più lavoro. Ecco il codice che ho finora.Come si inserisce una barra di ricerca in una finestra di avviso?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <SeekBar android:max="48" android:layout_height="wrap_content" android:id="@+id/seekBar1" android:layout_width="fill_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_marginTop="74dp"></SeekBar> 
    <TextView android:text="Change Hour Range" android:layout_height="wrap_content" android:id="@+id/textView1" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"></TextView> 
    <TextView android:text="Only most recent positions" android:layout_height="wrap_content" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="15dp"></TextView> 

</RelativeLayout> 

Ecco la finestra di avviso che ho finora

new AlertDialog.Builder(ImTracking.this) 
        //is there something like setcontentview for alert dialogs? 

        .setPositiveButton("Cancel", null) 
        .setNegativeButton("OK", 
          new DialogInterface.OnClickListener() { 
// does some work here 

risposta

13

Yep builder.setView(View v); ecco come si può utilizzare.

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View layout = inflater.inflate(R.layout.yourLayoutId, (ViewGroup) findViewById(R.id.yourLayoutRoot)); 
    AlertDialog.Builder builder = new AlertDialog.Builder(this) 
    .setView(layout); 
    AlertDialog alertDialog = builder.create(); 
    alertDialog.show(); 
    SeekBar sb = (SeekBar)layout.findViewById(R.id.yourSeekBar); 
    sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){ 
      //Do something here with new value 
     } 
    }); 

Modifica: Aggiunto progressListener al codice di esempio. Si noti che non è possibile ottenere un riferimento a SeekBar fino a dopo si chiama alertDialog.show(), se SeekBar non viene visualizzato findViewById() restituirà null. Si noti inoltre che è necessario utilizzare layout.findViewById(); poiché SeekBar è figlio di RelativeLayout a cui "layout" è un riferimento.

+0

Come potete vedere, ho un SeekBar all'interno del layout finestra di dialogo personalizzata. Dove inserisco i metodi per la barra di ricerca (onprogress modificato ecc.) – Sean

+0

Puoi impostare un progressListener come faresti normalmente. Vedi modifica. – FoamyGuy

+0

@Tim ottima risposta ... grazie .. – vnshetty

2
//This is the code you seek! 

public void ShowDialog(){ 
    final AlertDialog.Builder popDialog = new AlertDialog.Builder(this); 
final SeekBar seek = new SeekBar(this); 
    seek.setMax(255); 
    seek.setKeyProgressIncrement(1); 

popDialog.setIcon(android.R.drawable.btn_star_big_on); 
popDialog.setTitle("Please Select Into Your Desired Brightness "); 
popDialog.setView(seek); 


seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 



public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){ 


    txtView.setText("Value of : " + progress); 
    } 

Oppure si può guardare su questo tutorial

Problemi correlati