我想在我的应用程序中使用带有搜索栏的对话框。 但我真的不知道该怎么做,因为我缺乏使用 android 的经验。
因此,当您按下一个按钮时:应该出现一个带有搜索栏的对话框,用户可以输入一个值,然后按下 OK 按钮。
我现在的代码是developer.android默认的代码:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
我应该如何添加一个SeekBar
?
谢谢!
最佳答案
也许您可以考虑创建一个自定义对话框;它需要更多的工作,但它通常对我有用;) 为您的对话框创建一个新的布局文件(例如,your_dialog.xml):
<RelativeLayout
android:id="@+id/your_dialog_root_element"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<SeekBar
android:id="@+id/your_dialog_seekbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</SeekBar>
<Button
android:id="@+id/your_dialog_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</Button>
然后,在您的 Activity 中:
Dialog yourDialog = new Dialog(this);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.your_dialog, (ViewGroup)findViewById(R.id.your_dialog_root_element));
yourDialog.setContentView(layout);
因此,您可以按如下方式操作您的元素:
Button yourDialogButton = (Button)layout.findViewById(R.id.your_dialog_button);
SeekBar yourDialogSeekBar = (SeekBar)layout.findViewById(R.id.your_dialog_seekbar);
// ...
等等,为按钮和搜索栏设置监听器。
编辑: seekbar onchangelistener 应该如下所示:
OnSeekBarChangeListener yourSeekBarListener = new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//add code here
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//add code here
}
@Override
public void onProgressChanged(SeekBar seekBark, int progress, boolean fromUser) {
//add code here
}
};
yourDialogSeekBar.setOnSeekBarChangeListener(yourSeekBarListener);
关于Android,对话框中的SeekBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6424032/