1

そのタイトルが私がやりたいことをどれだけうまく説明しているかはわかりませんが、ここに行きます。基本的に、ボタンのリストを実用的に作成するアプリがあります。ボタンをクリックすると、説明が返されます。

次のクラスを作成しました

public class DynamicOnClickListener implements OnClickListener
{

    String description;
    public DynamicOnClickListener(String adesc) {
        //sets the description attribute at instantiation
         this.description = adesc;
    }

    public void onClick(View v) {
        //on button click returns dialog box with description in it
        Log.v("DynamicOnClickListener","1");
        AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
        builder.setMessage(description);
        builder.setCancelable(false);
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) 
                   {
                       dialog.cancel();
                   }
               });
        AlertDialog alert = builder.create();   
        alert.show();
    }

 }

これはそのままで完全に機能しますが、ダイアログボックスを少しジャズアップしたいと思います。私はオンラインでいくつかの例を見てきましたが、Androidドキュメントによると、カスタムxmlレイアウトを定義し、LayourInflatorを使用してカスタムxmlをダイアログのビューとして設定することを提案しています。(それはとにかく私がそれを理解した方法です、おそらく間違って十分に正しいです)

ドキュメントの例は私のものとは少し異なりますが、彼らの例によると、次の行を追加する必要があります

// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();

// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_signin, null))

ただし、これをクラスに追加すると、次のようになり、getActivity()でエラーが発生します。

public class DynamicOnClickListener implements OnClickListener
{

    String description;
    public DynamicOnClickListener(String adesc) {
        //sets the description attribute at instantiation
         this.description = adesc;
    }

    public void onClick(View v) {
        //on button click returns dialog box with program description in it
        Log.v("DynamicOnClickListener","1");
        AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());

        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(inflater.inflate(R.layout.description_dialog, null));


        builder.setMessage(description);
        builder.setCancelable(false);
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) 
                   {
                       dialog.cancel();
                   }
               });
        AlertDialog alert = builder.create();   
        alert.show();
    }

 }
4

3 に答える 3

3

メールを入力するためのEditTextと、データを送信してキャンセルするためのボタンを使用してダイアログボックスを作成しました。このダイアログボックスは、背景を暗くして表示されます。そして、現在のアクティビティの上にそれを表示します。

    final Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
               LinearLayout popUp = new LinearLayout(this);
               popUp.setBackgroundColor(Color.LTGRAY);
               popUp.setOrientation(1);

                EditText t1 = new  EditText(this);
                t1.setHint("Enter Your Email ID ");
                t1.setTextColor(Color.parseColor("#FFFFFF"));
                t1.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);          

                LinearLayout btnLayout = new LinearLayout(this);
                btnLayout.setBackgroundColor(Color.LTGRAY);
                btnLayout.setOrientation(0);
                btnLayout.setGravity(Gravity.CENTER);

                Button send = new  Button(this);
                send.setText("Send");
                send.setTextColor(Color.WHITE);
                Button cancel = new  Button(this);
                cancel.setText("Cancel");
                cancel.setTextColor(Color.WHITE);

                btnLayout.addView(send);
                btnLayout.addView(cancel);
                popUp.addView(t1);
                popUp.addView(btnLayout);

                dialog.setContentView(popUp);
              cancel.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });
                dialog.show();
于 2013-12-07T11:07:02.507 に答える
2
final Context mContext = v.getContext()
inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
builder.setView(inflater.inflate(R.layout.description_dialog, null));

これを試して。

于 2013-03-26T11:44:18.880 に答える
0

CoderDecoderとkleopatraの回答によると、ここに形状が更新されたバージョンがあります。

    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    GradientDrawable shape = new GradientDrawable();
    shape.setColor(getResources().getColor(R.color.shape_client_textview_background_color));
    shape.setStroke(10, getResources().getColor(R.color.shape_client_textview_border_color));
    shape.setCornerRadius(50);

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    LinearLayout popUp = new LinearLayout(this);
   // popUp.setBackgroundColor(Color.LTGRAY);
    layoutParams.setMargins(20,20,20,20);
    popUp.setOrientation(LinearLayout.VERTICAL);
    popUp.setLayoutParams(layoutParams);
    popUp.setBackground(shape);
    popUp.setPadding(20,20,20,20);

    EditText t1 = new EditText(this);
    t1.setHint("Enter Your Email ID ");
    t1.setTextColor(Color.parseColor("#FFFFFF"));
    t1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);

    LinearLayout btnLayout = new LinearLayout(this);
 //   btnLayout.setBackgroundColor(Color.LTGRAY);
    btnLayout.setOrientation(LinearLayout.HORIZONTAL);
    btnLayout.setGravity(Gravity.CENTER);

    Button send = new Button(this);
    send.setText("Send");
    send.setTextColor(Color.WHITE);
    Button cancel = new Button(this);
    cancel.setText("Cancel");
    cancel.setTextColor(Color.WHITE);

    btnLayout.addView(send);
    btnLayout.addView(cancel);
    popUp.addView(t1);
    popUp.addView(btnLayout);

    dialog.setContentView(popUp);
    cancel.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            dialog.dismiss();
        }
    });

    dialog.show();

結果は次のとおりです。

形のダイナミックダイアログ

于 2017-12-05T08:27:43.597 に答える