问题描述
我设计了一个自定义layout
为我的dialog
list
,使用户选择从只有一个选项list
,我想用它代替默认的dialog
。
我真的不知道该怎么做。
我有如下所示的fragment
interface
;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.widget.Toast;`
public class SingleChoiceClass extends DialogFragment {`
final CharSequence[] items = {"b1", "b2", "b3", "b4"};
String selection;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());`
builder.setTitle("Choose").setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
switch (arg1){
case 0:
selection = (String) items[arg1];
break;
case 1:
selection = (String) items[arg1];
break;
case 2:
selection = (String) items[arg1];
break;
case 3:
selection = (String) items[arg1];
break;
}
}
}).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Display toast with the user's selection
Toast.makeText(getActivity(), "Your choice is : " + selection, Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
}
我的自定义layout
是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="350dp"
android:layout_height="350dp"
android:background="@color/button_material_light"
android:layout_gravity="center">
<TextView
android:layout_width="350dp"
android:layout_height="50dp"
android:text="@string/textview"
android:layout_gravity="top"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_marginTop="20dp"
android:textSize="30sp"
android:textAlignment="center"
android:textStyle="normal"
android:id="@+id/textview" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="match_parent"
android:layout_height="55dp"
android:text="@string/radio1"
android:checked="false"
android:id="@+id/radio1"
android:textSize="25sp"
android:background="#2bf308"
android:clickable="true" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="55dp"
android:checked="false"
android:text="@string/radio2"
android:id="@+id/radio2"
android:textSize="25sp"
android:background="#f4fd02"
android:clickable="true" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="55dp"
android:text="@string/radio3"
android:checked="false"
android:id="@+id/radio3"
android:textSize="25sp"
android:background="#fb8e35"
android:clickable="true" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="55dp"
android:text="@string/radio4"
android:checked="false"
android:id="@+id/radio4"
android:textSize="25sp"
android:background="#fc3434"
android:clickable="true" />
</RadioGroup>
<Button
android:layout_width="match_parent"
android:layout_height="65dp"
android:text="@string/button"
android:id="@+id/button2"
android:layout_gravity="center_horizontal"
android:textSize="25sp"
android:clickable="true" />
</LinearLayout>
代码工作正常,但我需要使用自定义dialog
而不是默认dialog
。
提前致谢。
1楼
请尝试这个
//Please try this
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog dailog = new AlertDialog(getActivity());`
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_layout, null);
dialog.setTitle("Choose").setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
switch (arg1){
case 0:
selection = (String) items[arg1];
break;
case 1:
selection = (String) items[arg1];
break;
case 2:
selection = (String) items[arg1];
break;
case 3:
selection = (String) items[arg1];
break;
}
}
}).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Display toast with the user's selection
Toast.makeText(getActivity(), "Your choice is : " + selection, Toast.LENGTH_SHORT).show();
}
});
dialog.setView(view);
return view;
}
2楼
我正在更新我的答案,因为您希望将ListView
作为自定义视图。
首先在您的custom
布局中添加一个listView,如下所示:
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
接下来,您将需要创建一个adapter
以设置为创建的ListView
,您可以在onCreateDialog()
方法中执行以下操作
public Dialog onCreateDialog(Bundle savedInstanceState) {
ListView listView;
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.custom_layout, null);
listView = (ListView)dialogView.findViewById(R.id.listView); // inflating from custom layout.
builder.setView(dialogView);
}
此外,您需要创建一个Adapter
并在适配器的getView()
方法中为行inflate
布局,然后将该适配器设置为ListView
。
为了处理listView
点击事件,您可以这样做,
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});