Android AlertDialog 대화 상자 상세 설명 및 인 스 턴 스
9003 단어 AndroidAlertDialog대화 상자
관계 도 는 다음 과 같다.
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(" 【 】 !");
}
});
}
}
읽 어 주 셔 서 감사합니다. 여러분 에 게 도움 이 되 기 를 바 랍 니 다.본 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.