2014-03-05 8 views
5

내 요구 사항은 사용자가 해당 번호가 텍스트보기와 두 번째에 인쇄해야하는 시간에 수 선택기에서 숫자를 선택할 때마다 같은 때마다 사용자가 이전에이 번호는 대화번호 선택 대화 상자를 만드는 방법은 무엇입니까?

... 
    ... 

    view = (TextView) findViewById(R.id.textView7); 

    LinearLayout L = (LinearLayout) findViewById(R.id.linearLayout5); 
    L.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      showdialog(); 
     } 
    }); 
} 

@Override 
public void onValueChange(NumberPicker picker, int oldVal, int newVal) { 
     Log.i("value is",""+newVal); 
} 

public void showdialog() 
{ 
    Dialog dialog = new Dialog(NameActivity.this); 
    View npView = getLayoutInflater().inflate(R.layout.multidialog, null); 
    final NumberPicker firPicker = 
      (NumberPicker) npView.findViewById(R.id.numberPicker1); 
    firPicker.setMaxValue(9); 
    firPicker.setMinValue(0); 
    final NumberPicker secPicker = 
      (NumberPicker) npView.findViewById(R.id.numberPicker2); 
    secPicker.setMaxValue(9); 
    secPicker.setMinValue(0); 
    final NumberPicker tirPicker = 
      (NumberPicker) npView.findViewById(R.id.numberPicker3); 
    tirPicker.setMaxValue(9); 
    tirPicker.setMinValue(0); 

    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("title"); 
    builder.setView(npView); 
    builder.setPositiveButton("Set", 
    new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      view.setText(String.valueOf(firPicker.getValue() *100 
        + secPicker.getValue() *10 + tirPicker.getValue())); 
     } 
    }); 

    builder.setNegativeButton("Cancel", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
     } 
    }); 
    dialog = builder.create(); 
    dialog.show(); 
} 
+0

당신이 여기에 소스 코드를 붙여 넣을 수있어? – Naveen

+0

http://stackoverflow.com/questions/17805040/android-how-to-create-a-number-picker-dialog – user2450263

+0

대화 상자를 열 때마다 기본 텍스트가 표시됩니다. – Kedarnath

답변

1
에 표시해야 선택보다 다시 대화 상자를 엽니 다
public class sample extends Activity{ 

NumberPicker MyNumPicker1, MyNumPicker2, MyNumPicker3; 
TextView txtMyText; 
AlertDialog alertdialog; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.sample_layout); 

    txtMyText = (TextView) findViewById(R.id.txtMyText); 
    txtMyText.setText("5"); 

    txtMyText.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       View v1 = inflater.inflate(R.layout.numpicker, null); 
       MyNumPicker1 = (NumberPicker) v1.findViewById(R.id.MyNunPicker1); 
       MyNumPicker2 = (NumberPicker) v1.findViewById(R.id.MyNunPicker2); 
       MyNumPicker3 = (NumberPicker) v1.findViewById(R.id.MyNunPicker3); 

       MyNumPicker1.setMaxValue(20); 
       MyNumPicker1.setMinValue(1); 
       MyNumPicker1.setValue(Integer.parseInt(txtMyText.getText().toString())); 
       MyNumPicker1.setWrapSelectorWheel(true); 

       MyNumPicker2.setMaxValue(20); 
       MyNumPicker2.setMinValue(1); 
       MyNumPicker2.setValue(Integer.parseInt(txtMyText.getText().toString())); 
       MyNumPicker2.setWrapSelectorWheel(true); 

       MyNumPicker3.setMaxValue(20); 
       MyNumPicker3.setMinValue(1); 
       MyNumPicker3.setValue(Integer.parseInt(txtMyText.getText().toString())); 
       MyNumPicker3.setWrapSelectorWheel(true); 


       AlertDialog.Builder builder = new AlertDialog.Builder(sample.this); 

       builder.setView(v1); 
       builder.setTitle("Select Number"); 
       builder.setPositiveButton("Set", new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         int NumVal1 = MyNumPicker1.getValue(); 
         int NumVal2 = MyNumPicker2.getValue(); 
         int NumVal3 = MyNumPicker3.getValue(); 

         txtMyText.setText(""+ ((NumVal1 * 100) + (NumVal2 * 10) + NumVal3)); 
        } 
       }); 

       builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         alertdialog.dismiss(); 

        } 
       }); 

       alertdialog = builder.create(); 
       alertdialog.show(); 

      } 
     }); 
} 

} 

여기 number_picker.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<NumberPicker 
    android:id="@+id/MyNunPicker2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" /> 

<NumberPicker 
    android:id="@+id/MyNunPicker3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/MyNunPicker2" /> 

<NumberPicker 
    android:id="@+id/MyNunPicker1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_toLeftOf="@+id/MyNunPicker2"/> 

</RelativeLayout> 
+0

늦게 내 대답에 대한 미안, 대화 상자에서 3 숫자 선택기를 사용하고, 어떻게 쓸 수 있습니다. 코드를 주실 수 있나요, 고마워요. – user3363368

+0

wt shd는 다른 2 숫자 선택기와 함께 수행해야합니까? 우리는 [채팅 룸] (http://chat.stackoverflow.com/rooms/49109/number-picker) – kevz

+0

에서 이야기 할 수 있습니다. 미안하지만 적어도 20 개의 평판이 필요하기 때문에 대화방에서 이야기 할 수 없습니다. 나는 단 15 명 밖에 없다. – user3363368

관련 문제