开发者

Clickable view / onclick: Correct approach to make View clickable?

The following code will make the View clickable, but I am wondering if this is the correct approach to make a custom view clickable?

Code:

public class NodePickup extends LinearLayout
{
 public NodePickup(Context context, AttributeSet at开发者_JAVA技巧tributeSet)
 {
  super(context, attributeSet);

  LayoutInflater inflater = (LayoutInflater)     context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.nodepickup, this);

        this.setOnClickListener(new OnClickListener() 
        {
         @Override
   public void onClick(View v)
   {
          AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
          builder.setMessage("Ajabaja!")
          .setCancelable(true)
          .setPositiveButton("JA!", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) 
                    {
                     dialog.cancel();
                    }
                });
          builder.show();
   }
  });
 }
}


The code in onClick() is simply creating the dialog - there's nothing there that would cause it to get displayed on screen. To make this work, call showDialog(int) in your click handler and implement onCreateDialog(int) in your activity.

Check out the Creating Dialogs section of the Android docs for more information.


Calling setOnClickListener() is the appropriate way of making a view clickable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜