Android AlertDialog 대화 상자 상세 설명 및 인 스 턴 스

Android  AlertDialog


관계 도 는 다음 과 같다.
Android 는 주로 네 가지 대화 상 자 를 제공 합 니 다.
1:AlertDialog:기능 이 가장 풍부 하고 실제 응용 이 가장 넓 은 대화 상자 입 니 다.
2:ProgressDialog:진행 막대 대화 상자
3:DatePickerDialog:날짜 선택 기 대화 상자
4:TimePickerDialog:시간 선택 기 대화 상자
대화 상 자 를 만 드 는 단계:

AlertDialog.Builder builder = new AlertDialog.Builder(this)
        // 1:       
        .setTitle("         ")
            // 2:    
        .setIcon(R.drawable.tools)
            // 3:    
        .setMessage("        
"); // AlertDialog.Builder “ ” setPositiveButton(builder); // AlertDialog.Builder “ ” setNegativeButton(builder) .create() .show();
코드 영역:
main.xml 코드 영역:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center_horizontal">
<!--                -->
<EditText 
  android:id="@+id/show"
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:editable="false"/>
<!--             -->
<Button
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:text="     "
  android:onClick="simple"
  />
<!--             -->
<Button
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:text="        "
  android:onClick="simpleList"
  /> 
<!--             -->
<Button
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:text="        "
  android:onClick="singleChoice"
  /> 
<!--             -->
<Button
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:text="        "
  android:onClick="multiChoice"
  /> 
<!--             -->
<Button
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:text="         "
  android:onClick="customList"
  /> 
<!--             -->
<Button
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:text="   View   "
  android:onClick="customView"
  />         
</LinearLayout>
Activity 코드 영역:

public class MainActivity extends Activity {
  TextView show;
  String[] items = new String[] {
      "aserbao", "Android",
      " Java",
      "IOS" };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    show = (TextView) findViewById(R.id.show);
  }

  public void simple(View source)
  {
    AlertDialog.Builder builder = new AlertDialog.Builder(this)
      //        
      .setTitle("     ")
      //     
      .setIcon(R.drawable.tools)
      .setMessage("        
"); // AlertDialog.Builder “ ” setPositiveButton(builder); // AlertDialog.Builder “ ” setNegativeButton(builder) .create() .show(); } public void simpleList(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) // .setTitle(" ") // .setIcon(R.drawable.tools) // .setItems(items, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText(" 《" + items[which] + "》"); } }); // AlertDialog.Builder “ ” setPositiveButton(builder); // AlertDialog.Builder “ ” setNegativeButton(builder) .create() .show(); } public void singleChoice(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) // .setTitle(" ") // .setIcon(R.drawable.tools) // , ( 1) .setSingleChoiceItems(items, 1, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText(" 《" + items[which] + "》"); } }); // AlertDialog.Builder “ ” setPositiveButton(builder); // AlertDialog.Builder “ ” setNegativeButton(builder) .create() .show(); } public void multiChoice(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) // .setTitle(" ") // .setIcon(R.drawable.tools) // , 2 、 4 .setMultiChoiceItems(items , new boolean[]{false , true ,false ,true}, null); // AlertDialog.Builder “ ” setPositiveButton(builder); // AlertDialog.Builder “ ” setNegativeButton(builder) .create() .show(); } public void customList(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) // .setTitle(" ") // .setIcon(R.drawable.tools) // .setAdapter(new ArrayAdapter<String>(this , R.layout.array_item , items), null); // AlertDialog.Builder “ ” setPositiveButton(builder); // AlertDialog.Builder “ ” setNegativeButton(builder) .create() .show(); } public void customView(View source) { // app\src\main\res\layout\login.xml TableLayout loginForm = (TableLayout)getLayoutInflater() .inflate( R.layout.login, null); new AlertDialog.Builder(this) // .setIcon(R.drawable.tools) // .setTitle(" View ") // View .setView(loginForm) // “ ” .setPositiveButton(" ", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // } }) // “ ” .setNegativeButton(" ", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // , } }) // .create() .show(); } private AlertDialog.Builder setPositiveButton( AlertDialog.Builder builder) { // setPositiveButton “ ” return builder.setPositiveButton(" ", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText(" 【 】 !"); } }); } private AlertDialog.Builder setNegativeButton( AlertDialog.Builder builder) { // setNegativeButton “ ” return builder.setNegativeButton(" ", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText(" 【 】 !"); } }); } }
읽 어 주 셔 서 감사합니다. 여러분 에 게 도움 이 되 기 를 바 랍 니 다.본 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기