学习日记--dialog的基本用法测试

来源:互联网 发布:php保留两位小数 编辑:程序博客网 时间:2024/04/30 19:39

本测试实现了dialog的基本选择,单项选择,多项选择以及自定义选择的简单实例:

昨天的测试中:menu的一些item写在了java代码里面,所以不是很规范,应该写在res的menu文件夹下,把代码尽可能的拆分开,后期维护比较方便一点。


效果如下:



一、menu/main.xml (相关menu的一些item)

<menu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    tools:context="comhxzy.menu_dialog.MainActivity" >    <item        android:id="@+id/self_define_list"        android:orderInCategory="95"        android:showAsAction="never"        android:title="自定义list"/>    <item        android:id="@+id/self_define"        android:orderInCategory="96"        android:showAsAction="never"        android:title="自定义选项"/>    <item        android:id="@+id/multipleOption"        android:orderInCategory="97"        android:showAsAction="never"        android:title="多项选项"/>    <item        android:id="@+id/singeOption"        android:orderInCategory="98"        android:showAsAction="never"        android:title="单项选项"/>    <item        android:id="@+id/Option"        android:orderInCategory="99"        android:showAsAction="never"        android:title="选择"/></menu>



二、java代码

package comhxzy.menu_dialog;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.Context;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.content.DialogInterface.OnMultiChoiceClickListener;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemLongClickListener;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {private String[] data;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {/** * Programmatically opens the options menu. If the options menu * is already open, this method does nothing. */openOptionsMenu();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()) {// 自定义listcase R.id.self_define_list: {data = new String[50];Builder builder = new AlertDialog.Builder(this);LayoutInflater mLayoutInflater = getLayoutInflater();View view = mLayoutInflater.inflate(R.layout.list, null);ListView lv = (ListView) view.findViewById(R.id.listView);MyAdapter adapter = new MyAdapter(this, -1);lv.setAdapter(adapter);// 添加点击事件lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {Toast.makeText(MainActivity.this, "短按:" + position, Toast.LENGTH_SHORT).show();}});/** * 注意此方法的返回布尔值,如果为true,则停止当前点击事件(在当前测试中只会显示“长按+position”), * 如果为false,执行完之后还会执行其它点击事件(在当前测试中还会显示onItemClick点击事件toast显示: * ”短按:+position“,所以此方法的返回布尔值一般设为true。) */lv.setOnItemLongClickListener(new OnItemLongClickListener() {@Overridepublic boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long arg3) {Toast.makeText(MainActivity.this, "长按:" + position, Toast.LENGTH_SHORT).show();return true;}});builder.setTitle("自定义list");builder.setView(view);builder.create().show();break;}// 自定义选项case R.id.self_define: {/** * self_define为自己写的layout布局文件,根据自己的需求来写, * 本例包含了一个ImageView和一个TextView。 */LayoutInflater mLayoutInflater = getLayoutInflater();View view = mLayoutInflater.inflate(R.layout.self_define, null);Builder builder = new AlertDialog.Builder(this).setView(view);builder.create().show();break;}// 多项选项case R.id.multipleOption: {final String[] items = { "选项1", "选项2", "选项3" };/** * checkedItems数组的长度对应items数组的长度,初始可以设为false,如果 * 设成true,打开对话框的时候,如果设成true,数组下标对应的框是被勾选的 */final boolean[] checkedItems = { false, false, false };// 写成AlertDialog dialog = new AlertDialog.Builder(this)......// .creat();也可以,个人喜好;final Builder multiple = new AlertDialog.Builder(this).setMultiChoiceItems(items, checkedItems,new OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which, boolean isChecked) {}});AlertDialog dialog = multiple.create();dialog.setTitle("系统选项");dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "确定", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {String s = "";for (int i = 0; i < checkedItems.length; i++) {if (checkedItems[i]) {s = s + "" + items[i] + ",";}}Toast.makeText(getApplicationContext(), "选择了:" + s, 1).show();}});dialog.show();break;}// 单项选项case R.id.singeOption: {final String[] singe = { "选项1", "选项2", "选项3" };/** * 此处的-1代表初始化都不勾选; 设置0到在数组长度-1之内的数,则初始化对应数组下标进行勾选; * 如果设置的数字大于等于数组长度,默认也为-1 */AlertDialog dialog = new AlertDialog.Builder(this).setSingleChoiceItems(singe, -1, new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(getApplicationContext(), singe[which], 0).show();dialog.dismiss();}}).create();dialog.setTitle("singeOption");dialog.show();break;}// 基本选项case R.id.Option: {final String[] items = { "选项1", "选项2", "选项3" };Builder builder = new AlertDialog.Builder(this).setItems(items, new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {myTask(items[which]);}});AlertDialog dialog = builder.create();dialog.setTitle("Option");dialog.show();break;}default:break;}return super.onOptionsItemSelected(item);}public void myTask(final String s) {Builder builder = new AlertDialog.Builder(this);// PositiveButton为确定的按钮builder.setPositiveButton("确定:" + s, new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(getApplicationContext(), s, 0).show();}});// PositiveButton为否定的按钮builder.setNegativeButton("取消", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});AlertDialog dialog = builder.create();dialog.setTitle("系统提示");dialog.setMessage("请确认你的选择");dialog.show();}private class MyAdapter extends ArrayAdapter<String> {LayoutInflater inflater;public MyAdapter(Context context, int resource) {super(context, resource);inflater = LayoutInflater.from(getApplicationContext());}@Overridepublic int getCount() {return data.length;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if (null == convertView) {convertView = inflater.inflate(R.layout.list_item, null);}TextView text = (TextView) convertView.findViewById(R.id.text);text.setText("data" + position);return convertView;}}}

三.layout布局文件

1.activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="comhxzy.menu_dialog.MainActivity" >   <Button       android:id="@+id/button"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:layout_alignParentRight="true"       android:layout_alignParentTop="true"       android:layout_marginRight="18dp"       android:layout_marginTop="30dp"        android:text="点击出现菜单"/></RelativeLayout>


2.list_item.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ImageView        android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/ic_launcher" />    <TextView        android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>


3.list.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <ListView        android:id="@+id/listView"        android:layout_width="match_parent"        android:layout_height="match_parent" >    </ListView></LinearLayout>


4.self_define.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" >    <ImageView        android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:src="@drawable/ic_launcher" />    <Button        android:id="@+id/button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/image"        android:layout_centerInParent="true"        android:text="自定义按钮" /></RelativeLayout>





0 0
原创粉丝点击