問題描述
我希望在單擊按鈕后彈出一個(gè)警告對話框以顯示搜索欄,以便人們可以將值從 1 更改為 48.我制作了一個(gè)自定義 xml,它有一個(gè)搜索欄和兩個(gè)文本視圖,我希望對話框有兩個(gè)按鈕,一個(gè)只是取消對話框,另一個(gè)做更多的工作.這是我到目前為止的代碼.
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>
這是我目前看到的警報(bào)對話框
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
推薦答案
是的 builder.setView(View v);
下面是如何使用它.
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
}
});
在示例代碼中添加了progressListener.請注意,在調(diào)用 alertDialog.show() 之前,您無法獲得對 SeekBar 的引用,如果 SeekBar 未顯示 findViewById() 將返回 null.另請注意,您必須使用 layout.findViewById();
因?yàn)?SeekBar 是layout"引用的 RelativeLayout 的子級.
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.
這篇關(guān)于如何在警報(bào)對話框中放置搜索欄?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!