有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

对话框中的java Android动态表单

我正试图在我的安卓应用程序的对话框窗口中创建一个小表单,但下面的代码不起作用。我该怎么表现出来。它需要动态构建

谢谢

   @Override
   public void onClick(View v) {

    TextView myTV1=new TextView(getBaseContext());
    TextView myTV2=new TextView(getBaseContext());
    TextView myTV3=new TextView(getBaseContext());
    EditText myET1=new EditText(getBaseContext());

    myTV1.setText("hello");
    myTV2.setText("world");
    myTV3.setText("now");
    myET1.setText("Enter your name please");

    TableLayout t = new TableLayout(getBaseContext());
    t.addView(myTV1);
    t.addView(myTV2);
    t.addView(myTV3);
    t.addView(myET1);


    Dialog dialog = new Dialog(getBaseContext());
    dialog.setContentView(t);

    dialog.show();


   }

共 (1) 个答案

  1. # 1 楼答案

    我通常用这个
    我创建了一个带有自定义布局的对话框

       final Dialog dialog = new Dialog(LugarActivity.this);
                dialog.setContentView(R.layout.dialog_valoracion);
                dialog.setTitle("Valorar Lugar");
    
    
                // set the custom dialog components - text, image and button
                final RatingBar ratingBarLugar = (RatingBar)            dialog.findViewById(R.id.ratingBarLugar);
                Button btnValorar = (Button)   dialog.findViewById(R.id.btn_enviar_valoracion);
                // if button is clicked, close the custom dialog
                btnValorar.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        //code here
                    }
                });
    
                Button btnCancelar = (Button) dialog.findViewById(R.id.btn_cancelar_valoracion);
                btnCancelar.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
    
                dialog.show();
    

    在布局中

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <RatingBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ratingBarLugar"
        android:layout_gravity="center_horizontal"
        android:numStars="5"
        android:stepSize="1"/>
    
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">
    
        <Button
            android:id="@+id/btn_enviar_valoracion"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_enviar_valoracion"/>
    
        <Button
            android:id="@+id/btn_cancelar_valoracion"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btn_enviar_valoracion"
            android:text="@string/btn_cancelar"/>
    
        </RelativeLayout>
    

    我希望我帮了忙:)