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

Android菜鸟在成长-对话框显示列表项

2017年09月19日 ⁄ 综合 ⁄ 共 2508字 ⁄ 字号 评论关闭

 有图有真相!!!

第一种实现方法

布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MyLayout"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/mych"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

    <Button
        android:id="@+id/mybut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="选择水果" />
    
</LinearLayout>

package com.example.dialog1;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.support.v4.app.DialogFragment;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	private Button mybut=null;//定义按钮组件
	private TextView maych=null;//定义文本显示组件
	private String fruitData[]=new String[]{"苹果","西瓜","水蜜桃"};
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.mybut=(Button)super.findViewById(R.id.mybut);//取得组件
		this.maych=(TextView)super.findViewById(R.id.mych);//取得组件
		this.mybut.setOnClickListener(new OnClickListenerImpl());//设置单机事件
	}
	private class OnClickListenerImpl implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO 自动生成的方法存根
			Dialog dialog=new AlertDialog.Builder(MainActivity.this)//实列化对向
			.setTitle("请你选择你喜欢的水果")
			.setNegativeButton("取消",
					new DialogInterface.OnClickListener(){//设置操作监听
				public void onClick(DialogInterface dialog,
						int whichButton){//单机事件
					
				}
			}).setItems(fruitData,
					new DialogInterface.OnClickListener() {//设置操作监听
						
						@Override
						public void onClick(DialogInterface dialog, int which) {//单机事件
							// TODO 自动生成的方法存根
							
						}
					}).create();
			dialog.show();
		}
		
	}

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

}

第二种实现方法

定义资源文件   res\value\fruit_data.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="fruit_labels">
        <item >苹果</item>
        <item >西瓜</item>
        <item >水蜜桃</item>
        
    </string-array>
    
</resources>

修改Activity程序,让其可以直接从资源文件中读取列表选项

.setItems(R.array.fruit_labels,//设置列表选项
					new DialogInterface.OnClickListener() {//设置操作监听
						
						@Override
						public void onClick(DialogInterface dialog, int which) {//设置显示信息
							// TODO 自动生成的方法存根
							MainActivity.this.maych
							.setText("你选择的水果是:"+MainActivity.this//扎到MyView对象
									.getResources()//读取资源
									.getStringArray(R.array.fruit_labels)[which]);//读取数组信息

抱歉!评论已关闭.