您的位置:首页 > 移动开发 > Android开发

Android开发之列表对话框的使用

2013-12-30 20:40 477 查看




在values.xml新建interest.xml文件

<?xml
version="1.0" encoding="utf-8"?>
<resources>
 
  
 
  <string-array
name="selinterest">
 
     
<item>篮球</item>
 
     
<item>游泳</item>
 
     
<item>足球</item>
 
     
<item>游戏</item>
 
 
</string-array>
 
  
</resources>

main.xml

<LinearLayout
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=".MainActivity"
 
 
android:orientation="vertical">
 
  
 
  
 
  <Button
 
     
android:id="@+id/interest"
 
     
android:layout_width="wrap_content"
 
     
android:layout_height="wrap_content"
 
     
android:text="选择爱好" />
 
  
 
 
 
  
 
 
<TextView 
 
     
android:id="@+id/mysel"
 
     
android:layout_width="wrap_content"
 
     
android:layout_height="wrap_content"/>
 
  
 
  

</LinearLayout>

.java文件

package
com.example.dialogdemo;

import
android.os.Bundle;
import
android.app.Activity;
import
android.app.AlertDialog;
import
android.app.Dialog;
import
android.content.DialogInterface;
import
android.view.KeyEvent;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.TextView;

public class MainActivity
extends Activity {
private Button
button=null;
private View
interests=null;
private TextView
mysel=null;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.button=(Button)
super.findViewById(R.id.interest);
this.mysel=(TextView)
super.findViewById(R.id.mysel);
//为我们的图片添加事件
this.button.setOnClickListener(new
OnClickListenerImp());
}
public class OnClickListenerImp
implements OnClickListener{

public void onClick(View arg0)
{
Dialog dialog=new
AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.ic_launcher)
.setTitle("选择您的兴趣爱好")
.setNegativeButton("取消", new
DialogInterface.OnClickListener() {
public void
onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method
stub
}
})
//设置多选提示框
.setItems(R.array.selinterest,
new DialogInterface.OnClickListener() {
public void
onClick(DialogInterface dialog, int which) {
MainActivity.this.mysel.setText("您选择的爱好是"+MainActivity.this.getResources().getStringArray(R.array.selinterest)[which]);
}
})
.create();
dialog.show();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: