现在的位置: 首页 > 综合 > 正文

在AlertDialog对话框中显示布局文件及添加监听器+setItems

2018年02月10日 ⁄ 综合 ⁄ 共 3069字 ⁄ 字号 评论关闭

以创建文件为例

一、资源里layout文件夹里的布局文件create_dialog.xml如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical" >
  <!-- 二选一 -->
<RadioGroup 
	android:id="@+id/radiogroup_create"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content">
	<!-- 创建文件选项 -->
	<RadioButton 
	android:layout_height="wrap_content" 
	android:layout_width="fill_parent" 
	android:text="@string/create_file" 
	android:id="@+id/create_file" />
	<!-- 创建文件夹选项 -->
	<RadioButton 
	android:layout_height="wrap_content" 
	android:layout_width="fill_parent" 
	android:text="@string/create_folder" 
	android:id="@+id/create_folder" />
</RadioGroup>  
	<!-- 文本框,供用户填写文件名称 -->
	<EditText 
	android:layout_height="wrap_content" 
	android:id="@+id/new_filename" 
	android:layout_width="fill_parent" 
	android:hint="@string/create_hint"
	android:singleLine="true" />	
</LinearLayout>

二、在代码膨胀出布局文件对象,并对布局文件里的控件添加监听器

1、获取系统布局文件膨胀器
     LayoutInflater mLI = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

2、根据资源里layout文件夹里的布局文件,膨胀出布局文件对象
     final LinearLayout mLL = (LinearLayout)mLI.inflate(R.layout.create_dialog, null);
3、获取布局文件中的控件
     RadioGroup mCreateRadioGroup = (RadioGroup)mLL.findViewById(R.id.radiogroup_create);
     final RadioButton mCreateFileButton = (RadioButton)mLL.findViewById(R.id.create_file);
     final RadioButton mCreateFolderButton = (RadioButton)mLL.findViewById(R.id.create_folder);
4、为多选按钮RadioGroup设置监听器,监听选择改变,并做出响应处理

mCreateRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    	{
    		//当选择改变时触发
			public void onCheckedChanged(RadioGroup arg0, int arg1) 
			{
				if(arg1 == mCreateFileButton.getId())
				{
					mChecked = 1;
				}else if(arg1 == mCreateFolderButton.getId())
				{
					mChecked = 2;
				}
			}   		
    	});

三、用布局文件创建AlertDialog对话框

//构建一个AlertDialog及其构建器
Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
//设置标题
mBuilder.setTitle("新建");
//设置对话框里的视图,这个视图就是前面膨胀出的布局文件对象
mBuilder.setView(mLL);
//设置肯定按钮的监听器,确定按钮点击后进行相应的处理
mBuilder.setPositiveButton("创建", new DialogInterface.OnClickListener()
{
   //设置创建按键的监听器,当被点击时,进行文件创建操作
	  public void onClick(DialogInterface dialog, int which) 
		{
				//或者用户输入的名称
				mNewFolderName = ((EditText)mLL.findViewById(R.id.new_filename)).getText().toString();
				if(mChecked == 1)
				{//创建文件
					try 
					{
						mCreateFile = new File(mCurrentFilePath+java.io.File.separator+mNewFolderName+".txt");
						mCreateFile.createNewFile();
					} 
					catch (IOException e) 
					{
					}
				}
				else if(mChecked == 2)
				{//创建文件夹
					mCreateFile = new File(mCurrentFilePath+java.io.File.separator+mNewFolderName);
					if(!mCreateFile.exists()&&!mCreateFile.isDirectory()&&mNewFolderName.length() != 0)
					{
						mCreateFile.mkdirs();
					}
					else
					{
						Toast.makeText(MainActivity.this, "文件名为空,还是重名了呢?", Toast.LENGTH_SHORT).show();
					}
				}			
	  }
});
//设置中立按键,第2个参数为空,标识没有设置监听器
mBuilder.setNeutralButton("取消", null);
//显示对话框
mBuilder.show();
四、通过setItems,可以直接显示列表内容,并可以添加列表中条目的监听事件
new AlertDialog.Builder(Traffic.this).setTitle(
					R.string.suggst_road).setItems(road_list,//设置标题:建议线路
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							//将信息显示到结果标签
							draw_road_table(road_list[which], false);
							//切换当前显示的标签为结果标签
							tabHost.setCurrentTabByTag("tab4");
						}
					}).create();

抱歉!评论已关闭.