問題描述
我想在我的應用程序中使用帶有搜索欄的對話框.但我真的不知道該怎么做,因為我缺乏使用 android 的經驗.
I would like to use a dialog with a seekbar in my application. But I don't really know how to do it because I lack experience with android.
因此,當您按下按鈕時:應該會出現一個帶有搜索欄的對話框,用戶可以輸入一個值,然后按 OK 按鈕.
So, when you press a button: a dialog should come up, with a seekbar and the user is able to enter a value and then press OK-button.
我現在的代碼是developer.android的默認代碼:
The code I have at the moment is the default code by 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):
Maybe you could think of create a custom dialog; it requires more work but it usually works for me ;) Create a new layout file for your dialog (say, 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>
然后,在你的活動中:
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);
因此,您可以對元素進行如下操作:
Thus, you can operate on your element as follows:
Button yourDialogButton = (Button)layout.findViewById(R.id.your_dialog_button);
SeekBar yourDialogSeekBar = (SeekBar)layout.findViewById(R.id.your_dialog_seekbar);
// ...
等等,設置按鈕和搜索欄的監聽器.
and so on, to set listeners for button and seekbar.
seekbar onchangelistener 應該如下:
The seekbar onchangelistener should be as follows:
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 在對話框中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!