2011-08-25 31 views
7

ボタンをクリックしてシークバーを表示してから、ユーザーが1-48の値を変更できるようにするために、警告ダイアログボックスを表示します。シークバーと2つのテキストビューを持つカスタムXMLを作成しました。ダイアログボックスには2つのボタンがあり、1つはダイアログをキャンセルし、もう1つはもっと機能します。ここに私がこれまで持っていたコードがあります。アラートダイアログにシークバーを配置するにはどうすればよいですか?

<?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> 

はここ、ここ、あなたがそれを使用することができる方法である私がこれまで

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 

答えて

13

うんbuilder.setView(View v);持つ警告ダイアログです。

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

編集:サンプルコードにprogressListenerを追加しました。 SeekBarが表示されていない場合は、の後にalertDialog.show()を呼び出すまでSeekBarへの参照を取得できないことに注意してください。findViewById()はnullを返します。 SeekBarはRelativeLayoutの子であるため、 'layout'が参照されるため、layout.findViewById();を使用する必要があります。

+0

上で見ることができます。どこにシークバーのメソッドを置くのですか?(変更された進行中の進行中) – Sean

+0

通常どおりにprogressListenerを設定できます。編集を参照してください。 – FoamyGuy

+0

@Tim素晴らしい答え...ありがとう.. – 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); 
    } 

それとも、あなたが見ることができるように、私はカスタムダイアログのレイアウトの内側にシークバーを持っているこのtutorial

関連する問題