Android 对话框片段中自己的布局
我正在测试“对话片段”,我做了一个小程序,当它开始显示一个包含3个选项的对话片段时,主活动上的文本视图将显示选择了哪个选项。所以基本上是对话和活动之间的交流。我一直在用2个按钮(正片和负片)做例子,但现在我用我自己的3个按钮的布局进行测试,我不知道如何继续 让我们看看代码: 对话框_layout.xml中的3按钮Android 对话框片段中自己的布局,android,android-fragments,android-alertdialog,Android,Android Fragments,Android Alertdialog,我正在测试“对话片段”,我做了一个小程序,当它开始显示一个包含3个选项的对话片段时,主活动上的文本视图将显示选择了哪个选项。所以基本上是对话和活动之间的交流。我一直在用2个按钮(正片和负片)做例子,但现在我用我自己的3个按钮的布局进行测试,我不知道如何继续 让我们看看代码: 对话框_layout.xml中的3按钮 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layo
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
</LinearLayout>
主要活动是什么
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends FragmentActivity implements
TwoActionButtonsDialog.DialogListener {
private static final String TAG = "dialog";
private TextView texto = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
texto = (TextView) findViewById(R.id.textView1);
showTwoActionButton();
}
public void showTwoActionButton() {
DialogFragment dialog = new TwoActionButtonsDialog();
dialog.show(getSupportFragmentManager(), TAG);
}
// The dialog fragment receives a reference to this Activity through the
// Fragment.onAttach() callback, which it uses to call the following methods
// defined by the NoticeDialogFragment.NoticeDialogListener interface
@Override
public void onDialogOption1(DialogFragment dialog) {
// User touched the dialog's positive button
texto.setText("1");
}
@Override
public void onDialogOption2(DialogFragment dialog) {
// User touched the dialog's negative button
texto.setText("2");
}
@Override
public void onDialogOption3(DialogFragment dialog) {
// TODO Auto-generated method stub
texto.setText("3");
}
}
我不知道如何管理按钮,因为我不能:
Button btn1 = ((Button) findViewById(R.id.button1));
Button btn2 = (Button) findViewById(R.id.button2);
Button btn3 = (Button) findViewById(R.id.button3);
它抛出一个错误:“类型TwoActionButtonsDialog的findViewById(int)方法未定义”。如果我夸大了布局,为什么我不能接近它们
我该怎么办?为对话框膨胀视图后,保存结果:
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.dialog_layout, null)
builder.setTitle("Test").setView(v);
之后,您可以遍历视图中的按钮:
Button btn1 = (Button) v.findViewById(R.id.button1);
请发布日志。它有助于快速定位错误。基本上,您正在尝试弹出一个带有三个选择按钮的对话框?
Button btn1 = (Button) v.findViewById(R.id.button1);