600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > android 自定义含有滚动选择器的对话框

android 自定义含有滚动选择器的对话框

时间:2023-07-05 06:32:54

相关推荐

android 自定义含有滚动选择器的对话框

最近在写一个项目,需要用到滚动选择器,本人的想法是弹出一个对话框,中间包含滚动选择器,其中滚动选择器的源码参考的这篇文章/zhongkejingwang/article/details/38513301

先来看一下效果

用滚动选择器选择分数,点击确定按钮,打分完成,点击取消按钮,对话框消失。

关于自定义滚动选择器大家可以参考上面那篇文章,我觉得写的很容易理解。本人主要把滚动选择器和对话框相结合。

下面我们来看代码

PickerView的代码完全来自于上面文章的博主,这里就不贴了。

对话框布局文件如下

activity_main.layout

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:clickable="true"android:orientation="vertical"android:padding="10.0dip"><LinearLayout android:id="@+id/customDialog"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:layout_gravity="center"><TextView android:layout_width="match_parent"android:layout_height="40.0dip"android:gravity="center"android:textColor="#aa00aeef"android:textSize="20sp"android:text="开始打分"/><LinearLayout android:id="@+id/content"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:orientation="horizontal"><com.example.flt.myapplication.PickerViewandroid:id="@+id/picker"android:layout_width="100dp"android:layout_height="100dp"></com.example.flt.myapplication.PickerView><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="分"android:layout_gravity="center_vertical"/></LinearLayout></LinearLayout></FrameLayout>

MainActivity的代码如下

package com.example.flt.myapplication;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.Toast;import java.util.ArrayList;public class MainActivity extends Activity implements View.OnClickListener {private Button button;public PickerView pickerView;public String fenshu;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.login);button = (Button)this.findViewById(R.id.login);button.setOnClickListener(this);}/*** 点击登录按钮弹出对话框* @param v*/@Overridepublic void onClick(View v) {switch (v.getId()){case R.id.login:dialog();Toast.makeText(this,"点击了",Toast.LENGTH_LONG).show();}}//对话框public void dialog(){final AlertDialog.Builder builder = new AlertDialog.Builder(this);//初始化自定义布局参数LayoutInflater layoutInflater = getLayoutInflater();final View customLayout = layoutInflater.inflate(R.layout.activity_main, (ViewGroup)findViewById(R.id.customDialog));//为对话框设置视图builder.setView(customLayout);pickerView = (PickerView)customLayout.findViewById(R.id.picker);//定义滚动选择器的数据项ArrayList<String> grade = new ArrayList<>();for(int i=0;i<10;i++){grade.add(i+"");}//为滚动选择器设置数据pickerView.setData(grade);pickerView.setOnSelectListener(new PickerView.onSelectListener() {@Overridepublic void onSelect(String text) {Log.i("tag","选择了"+text);fenshu = text;}});//对话框的确定按钮builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Log.i("分数为=",fenshu.toString());}});//对话框的取消按钮builder.setNegativeButton("取消",null);//显示对话框builder.show();}}

好啦~很简单吧

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。