简体   繁体   English

Android自定义对话框搜索栏

[英]Android custom dialog seekbar

I need my app to create a dialog Seekbar. 我需要我的应用程序来创建一个对话框Seekbar。 But I cannot make it work. 但我不能让它发挥作用。 I referring this stackoverflow question Android, SeekBar in dialog and here is my code. 在对话框中引用了这个stackoverflow问题Android,SeekBar ,这是我的代码。

I am creating a menu, and on menu item when user clicks size_config button it should show seekbar. 我正在创建一个菜单,当用户点击size_config按钮时,它会显示菜单项,它应该显示搜索栏。

 }if(item.getItemId() == R.id.size_config){
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.id.dialog, (ViewGroup)findViewById(R.id.layout));
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
        .setView(layout);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
        SeekBar sb = (SeekBar)layout.findViewById(R.id.dialog);
        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
                Log.d("Dialog to change size", "It's working");
            }

            @Override
            public void onStartTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }
        });
    }

And this is the layout file. 这是布局文件。

dialog.xml dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:padding="10dp"
   android:id="@+id/layout">

<SeekBar xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/dialog"
   android:layout_width="225dp"
   android:layout_height="wrap_content"/>

</LinearLayout>

Here is the error code. 这是错误代码。

 06-24 02:31:30.369: E/AndroidRuntime(379): FATAL EXCEPTION: main
 06-24 02:31:30.369: E/AndroidRuntime(379): android.content.res.Resources$NotFoundException: Resource ID #0x7f070001 type #0x12 is not valid
 06-24 02:31:30.369: E/AndroidRuntime(379):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:1874)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at android.content.res.Resources.getLayout(Resources.java:731)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at in.isuru.animation.SnowFall.onOptionsItemSelected(SnowFall.java:189)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at android.app.Activity.onMenuItemSelected(Activity.java:2195)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:137)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:874)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at android.widget.ListView.performItemClick(ListView.java:3382)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at  android.os.Handler.handleCallback(Handler.java:587)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at android.os.Handler.dispatchMessage(Handler.java:92)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at android.os.Looper.loop(Looper.java:123)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at android.app.ActivityThread.main(ActivityThread.java:4627)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at java.lang.reflect.Method.invokeNative(Native Method)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at java.lang.reflect.Method.invoke(Method.java:521)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 06-24 02:31:30.369: E/AndroidRuntime(379):     at dalvik.system.NativeStart.main(Native Method)

Here is how to add SeekBar dialog, 以下是添加SeekBar对话框的方法,

Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Set size!");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
dialog.show();

seekbar = (SeekBar) dialog.findViewById(R.id.size_seekbar);
final TextView tv_dialog_size = (TextView) dialog.findViewById(R.id.set_size_help_text);

// and you can call seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
      //implement methods.
}

And dialog layout xml is, 对话框布局xml是,

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/layout"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="vertical"
     android:padding="2dp"
     android:layout_gravity="center" >

<TextView
    android:id="@+id/set_size_help_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:text="Please select snowflake size"
    android:textSize="25dp" />

<SeekBar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/size_seekbar"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp" />

</LinearLayout>

This call: 这个电话:

View layout = inflater.inflate(R.id.dialog, (ViewGroup)findViewById(R.id.layout));

is wrong. 是错的。 You need R.layout.dialog (the layout ressource you want to inflate the view with) 你需要R.layout.dialog (你希望用视图来扩展视图的布局资源)

View layout = inflater.inflate(R.layout.dialog, null);

找到答案......对于那些带有自定义对话框的搜索栏,他们需要初始化为 -

SeekBar seekBar = (SeekBar)dialog.findViewById(R.id.valueBar);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM