1

ここに私が目指しているものがあります: 画像

これを正しく行っているかどうかはわかりません。おそらく、より効率的でクリーンな方法がありますが、その方法を知る必要があります。

このレイアウトは xml で設計され、インフレータを介して膨らませます。生成されたビューは AlertDialog に配置されました。したがって、これはユーザーからは AlertDialog と見なされます。

私の懸念は、下部のタグセクションにあります。これをTumblrのタグのように機能させたいと思っています。文字列を入力してボタンを押すと、そのタグ名を持つボタンがその下の空白のセクションに表示されます。

これらのボタン (それぞれのタグ名) をクリックすると、フレームから消えます。

いくつかの懸念があります。

  1. リスナーの実装に問題があります。AddTag ボタンが (現在は見えないが存在する) LinearLayout にさらにボタンを作成する場合、作成されたボタンはどうなるでしょうか? これらのボタンは、AddTag ボタンの onClick メソッドから定義された内部メソッドで作成された場合、LinearLayout から自身を削除する onClick リスナーをどのように実装しますか?

  2. ボタン メソッドと内部クラスで参照するために、これらのビューの一部を FINAL として宣言する必要があるのではないかと心配しています。このため、私は今立ち往生しています。

  3. タグ ボタンの独自のレイアウトを定義する必要がありますか? なるほど、LinearLayout は物事を次々に表示しますね。いくつかのソーシャル ネットワーキング サイトのやり方を再現してみたいと思います。レイアウトを上から下、左から右にボタンで埋めます。現在の行に空きがない場合は、次の行に移動してそこにタグ ボタンを追加します。これは基本的に、オートラッピングを備えた動的な LinearLayout です。

これを実装するためのより良い方法がある場合は、何をすべきかの一般的な手順を教えてください。私はまだ Fragments を学んでいませんが、ここで非常に役立つと思います。また、ViewGroup を拡張するクラスを作成し、そこに XML を拡張し、物事を処理するヘルパー メソッドを追加する必要がありますか? DialogFragment から addView (作成したばかりのクラス) を追加して、そこから作業できると思いますか?

ちなみに、これが私の現在のコードです。私は立ち往生して困惑しています。

/**
    * Opens a view for the user to define their new action and add it to the
    * dictionary.
    * 
    * @param view
    */
   public void defineNewAction(View view) {
      final AlertDialog.Builder builder = new AlertDialog.Builder(this);
      LayoutInflater inflater = this.getLayoutInflater();
      View viewToSet = inflater.inflate(
            R.layout.define_new_action_window_layout,
            null);      

      final EditText newActionName = (EditText) viewToSet
            .findViewById(R.id.set_action_name);

      final RadioGroup priorityGroup = (RadioGroup) viewToSet
            .findViewById(R.id.radiogroup_set_priority);

      final EditText goalTimeHours = (EditText) viewToSet
            .findViewById(R.id.set_goal_time_hours);

      final EditText goalTimeMinutes = (EditText) viewToSet
            .findViewById(R.id.set_goal_time_minutes);

      final EditText addTagsInput = (EditText) viewToSet
            .findViewById(R.id.add_tags_input);
      Button addTagButton = (Button) viewToSet.findViewById(R.id.btn_add_tags);
      final ArrayList<String> tags = new ArrayList<String>();

      final LinearLayout currentTagsLayout = (LinearLayout) viewToSet
            .findViewById(R.id.current_tags);
      addTagButton.setOnClickListener(new View.OnClickListener() {

         public void onClick(View v) {
            String tag = addTagsInput.getText().toString();
            tags.add(tag);
            Button newTag = new Button(builder.getContext());
            int tagId = tag.hashCode();
            if (tagId < 0)
               tagId *= -1;
            newTag.setId(tagId);

            newTag.setOnClickListener(new View.OnClickListener() {

               public void onClick(View v) {
                  Button toRemove = (Button) currentTagsLayout.findViewById(tagId);
                  currentTagsLayout.removeView(toRemove);
               }
            });

            currentTagsLayout.addView(newTag);
         }
      });

      builder.setTitle("Define your action.");
      builder.setView(viewToSet);
      builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

         public void onClick(DialogInterface arg0, int arg1) {
            String name = newActionName.getText().toString();
            int priority = priorityGroup.getCheckedRadioButtonId();
            int goalHours = Integer
                  .parseInt(goalTimeHours.getText().toString());
            int goalMinutes = Integer.parseInt(goalTimeMinutes.getText()
                  .toString());
         }

      });
      builder.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {

               public void onClick(DialogInterface arg0, int arg1) {

               }
            });
      AlertDialog dialog = builder.create();
      dialog.show();
   }
4

1 に答える 1

1

I have trouble implementing listeners

There's no trouble. For the functionality you are trying to achieve, you can keep adding buttons and setting OnClickListeners on them. You don't even need to give them an id, or track them in any way. The following code inside your OnClickListener will do:

newTag.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        // Use the View given to you
        currentTagsLayout.removeView(v);

    }
});

I am afraid about having to declare some of these views as FINAL

This is how Java works. I haven't noticed any crippling effects of this. You can also declare your variables as global to not have to define them as final. But I don't see why declaring them as final is an issue. Could you provide an example where this is a problem?

Do I have to define my own layout for the tag buttons?

This is something you will have to deal with yourself. It's a design decision. If you need auto-wrapping support, you can look at Android Flow Layout: Link. It's an extended LinearLayout that supports auto-wrap of its contents.

I have not learned Fragments yet, but I think it may be VERY applicable here

I don't see why they would be.

Note/Aside: Some kind of a check here would be better:

String tag = "";

if (!addTagsInput.getText().toString().equals("")) {
    tag = addTagsInput.getText().toString();
} else {
    // handle empty string
}
于 2013-09-10T17:56:18.120 に答える