我如何把搜索栏的警告对话框?对话框

2023-09-07 08:38:50 作者:不爱我 就别深拥!

我想要一个警告对话框弹出后,我点击一个按钮,有一个搜索栏,这样的人可以从1-48更改值。我做了一个自定义XML,有一个搜索栏和两个文本视图和我想的对话框中有两个按钮,一个刚刚取消对话框,另做更多的工作。这里是code我有这么远。

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:方向=垂直
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =FILL_PARENT>
    <搜索栏的Andr​​oid版本:最大=48机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID / seekBar1机器人:layout_width =FILL_PARENT机器人:layout_alignParentTop =真正的机器人:layout_alignParentLeft =真正的机器人:layout_marginTop =74dp>< /搜索栏>
    < TextView的Andr​​oid的:机器人:ATTR / textAppearanceLarge文本=更改小时范围机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID / textView1机器人textAppearance =机器人:layout_width =WRAP_CONTENT机器人:layout_alignParentTop =真正的机器人:layout_centerHorizo​​ntal =真正的>< / TextView的>
    < TextView的Andr​​oid版本:文本=只有最近的位置机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID / textView2机器人:layout_width =WRAP_CONTENT机器人:layout_below =@ + ID / textView1机器人:layout_centerHorizo​​ntal =真正的机器人:layout_marginTop =15dp>< / TextView的>

< / RelativeLayout的>
 

下面是我的警告对话框至今

 新AlertDialog.Builder(ImTracking.this)
                    //是有什么样的setContentView警报对话框?

                    .setPositiveButton(取消,NULL)
                    .setNegativeButton(OK,
                            新DialogInterface.OnClickListener(){
 //做一些工作在这里
 

解决方案

是的 builder.setView(视图V); 这里是如何使用它。

  LayoutInflater吹气=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    查看布局= inflater.inflate(R.layout.yourLayoutId,(ViewGroup中)findViewById(R.id.yourLayoutRoot));
    AlertDialog.Builder建设者=新AlertDialog.Builder(本)
    .setView(布局);
    AlertDialog alertDialog = builder.create();
    alertDialog.show();
    搜索栏SB =(搜索栏)layout.findViewById(R.id.yourSeekBar);
    sb.setOnSeekBarChangeListener(新SeekBar.OnSeekBarChangeListener(){
        公共无效onProgressChanged(搜索栏搜索栏,INT进步,布尔FROMUSER){
            //做的东西在这里有新的价值
        }
    });
 
992

编辑:添加progressListener品尝code。做笔记,你不能得到一个引用您的搜索栏,直到的在的调用alertDialog.show(),如果搜索栏不被显示findViewById()将返回null。另请注意,您必须使用 layout.findViewById(); ,因为搜索栏是RelativeLayout的一个孩子说'布局'是一个参考。

I want an alert dialog box to pop up after I click a button to have a seek bar so that the person can change the value from 1-48. I've made a custom xml that has a seek bar and two text view and I would like the dialog box to have two buttons, one just cancels the dialog and the other does more work. Here is the code I have so far.

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <SeekBar android:max="48" android:layout_height="wrap_content" android:id="@+id/seekBar1" android:layout_width="fill_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_marginTop="74dp"></SeekBar>
    <TextView android:text="Change Hour Range" android:layout_height="wrap_content" android:id="@+id/textView1" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"></TextView>
    <TextView android:text="Only most recent positions" android:layout_height="wrap_content" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="15dp"></TextView>

</RelativeLayout>

Here is the alert dialog that I have so far

new AlertDialog.Builder(ImTracking.this)
                    //is there something like setcontentview for alert dialogs?

                    .setPositiveButton("Cancel", null)
                    .setNegativeButton("OK",
                            new DialogInterface.OnClickListener() {
 // does some work here

解决方案

Yep builder.setView(View v); here is how you can use it.

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.yourLayoutId, (ViewGroup) findViewById(R.id.yourLayoutRoot));
    AlertDialog.Builder builder = new AlertDialog.Builder(this)
    .setView(layout);
    AlertDialog alertDialog = builder.create();
    alertDialog.show();
    SeekBar sb = (SeekBar)layout.findViewById(R.id.yourSeekBar);
    sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
            //Do something here with new value
        }
    });

Edit: Added progressListener to sample code. Do note that you cannot get a reference to your SeekBar until after you call alertDialog.show(), if the SeekBar is not being shown findViewById() will return null. Also note that you must use layout.findViewById(); because the SeekBar is a child of the RelativeLayout that 'layout' is a reference to.