0

Please help me to implment the Button Action in below the Class

   public class DoneButtonMain extends MainActivity implements OnClickListener{

        private Activity activity;
        private Context context;


        public DoneButtonMain(Activity activity) {
            this.activity=activity;
            // TODO Auto-generated constructor stub
        }


        public View prepareControl(){

            Button btn=new Button(activity);
            btn.setText("Done");
            btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            btn.setBackgroundColor(Color.parseColor("#05a5a6"));
            btn.setTypeface(Typeface.DEFAULT_BOLD);
            btn.setPadding(7,7, 7, 7);

            btn.setTextColor(Color.parseColor("#ffffff"));

        return btn;


        }

    public LayoutParams prepareLayoutParam() {
        RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
        param.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
        param.rightMargin=20;
        param.bottomMargin=20;
        param.topMargin=20;
        return param;


    }
}

And MainActivity looks as below

public class MainActivity extends Activity {


        @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    LinearLayout parentlinLayout = new LinearLayout(this);
        parentlinLayout.setOrientation(LinearLayout.VERTICAL);
        LayoutParams linLayoutParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 


    RelativeLayout raltivelayout1=new RelativeLayout(this);
        raltivelayout1.setBackgroundColor(Color.parseColor("#1B4F91"));

      //Button Added to the relativeLayaout
        DoneButtonMain object=new DoneButtonMain(this);

        raltivelayout1.addView(object.prepareControl(),object.prepareLayoutParam());
        //Relative layout is added to the Linear View
        parentlinLayout.addView(raltivelayout1);
}

i have tried to button.setOnClickListener but i am getting a null pointer exception

4

1 回答 1

3

Thanks all, i got the sloution. as i have implemented the OnClickListener in class DoneButtonMain the prepareControl through activity object.

private void handleBtnClick() {
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

//required code
});
    }
于 2014-01-09T19:51:04.623 回答