您的位置:首页 > 其它

AlertDialogTest各种类型对话框

2015-12-19 11:20 260 查看
java代码

package com.example.zhang.alertdialogtest;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TableLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView show;
String[] items = new String[]{
"疯狂java讲义", "疯狂安卓讲义",
"疯狂英语讲义", "疯狂数学讲义"
};

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

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

public void simple(View source){
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("简单的对话框")
.setIcon(R.drawable.tools)
.setMessage("对话框的测试内容\n第二行内容");
//为AlertDialog.Builder添加“确定”按钮
setPositiveButton(builder);
//为AlertDialog.Builder添加“取消”按钮
setNegativeButton(builder)
.create()
.show();
}

//调用setItems()来设置简单列表项对话框
public void simpleList (View source){
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("简单列表项对话框")
.setIcon(R.drawable.tools)
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
show.setText("你选中了《" + items[which] + "》");
}
});
//为AlertDialog.Builder添加“确定”按钮
setPositiveButton(builder);
//为AlertDialog.Builder添加“取消”按钮
setNegativeButton(builder)
.create()
.show();
}

//调用AlertDialog.Builder的setSingleChoiceItems()方法来创建带单选列表项的对话框
public void singleChoice(View source){
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("单选列表项对话框")
.setIcon(R.drawable.tools)
//设置单选列表项,默认选中第二项(索引为1)
.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
show.setText("你选中了《" + items[which] + "》");
}
});
//为AlertDialog.Builder添加“确定”按钮
setPositiveButton(builder);
//为AlertDialog.Builder添加“取消”按钮
setNegativeButton(builder)
.create()
.show();
}

//调用AlertDialog.Builder的setSingleChoiceItems()方法来创建带单选列表项的对话框
public void multiChoice(View source){
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("多选列表项对话框")
.setIcon(R.drawable.tools)
//设置单选列表项,默认选中第二项(索引为1)
.setMultiChoiceItems(items, new boolean[]{true, false, false, false}, null);
//为AlertDialog.Builder添加“确定”按钮
setPositiveButton(builder);
//为AlertDialog.Builder添加“取消”按钮
setNegativeButton(builder)
.create()
.show();
}

//调用setAdapter()方法来创建自定义列表对话框
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)
.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
show.setText("你选中了《" + items[which] + "》");
}
});
setPositiveButton(builder);
setNegativeButton(builder)
.create()
.show();
}

public void customView(View source){
//加载login.xml文件布局
TableLayout loginForm = (TableLayout)getLayoutInflater().inflate(R.layout.login, null);
new AlertDialog.Builder(this)
.setTitle("自定义View")
.setIcon(R.drawable.tools)
.setView(loginForm).setPositiveButton("登陆", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//这里可以增加登陆
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//这里可以增加取消登录
}
})
.create()
.show();
}

//调用setPositiveButton方法添加“确定”按钮
private AlertDialog.Builder setPositiveButton(AlertDialog.Builder builder){
return builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
show.setText("单机了【确定】按钮!");
}
});
}

//调用setNegativeButton方法添加“取消”按钮
private AlertDialog.Builder setNegativeButton(AlertDialog.Builder builder){
return builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
show.setText("单击了【取消】按钮!");
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}


xml代码

<!--content_main-->

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.zhang.alertdialogtest.MainActivity"
tools:showIn="@layout/activity_main">

<EditText
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"/>
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="简单对话框"
android:onClick="simple"/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="简单列表项对话框"
android:onClick="simpleList"/>
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="单选列表项对话框"
android:onClick="singleChoice"/>
<Button
android:id="@+id/btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多选列表项对话框"
android:onClick="multiChoice"/>
<Button
android:id="@+id/btn5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义列表项对话框"
android:onClick="customList"/>
<Button
android:id="@+id/btn6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义View对话框"
android:onClick="customView"/>
</LinearLayout>

<!--array_item-->

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TextView"
android:textColor="#f0f"
android:textSize="30dp"
android:shadowColor="#ff0"
android:shadowRadius="2"
android:shadowDx="5"
android:shadowDy="5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<!--login-->

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loginForm"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="10pt"/>
<!-- 输入用户名的文本框 -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请填写登录账号"
android:selectAllOnFocus="true"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="10pt"/>
<!-- 输入密码的文本框 -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请填写密码"
android:password="true"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="电话号码:"
android:textSize="10pt"/>
<!-- 输入电话号码的文本框 -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请填写您的电话号码"
android:selectAllOnFocus="true"
android:phoneNumber="true"/>
</TableRow>
</TableLayout>


















内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: