关于片段上的单击按钮

时间:2016-07-29 05:11:51

标签: android android-fragments button onclicklistener

我是新来的,所以我想询问上面的标题,

我有这样的代码:

public class TanyaDokter extends Fragment {

View View;
public TanyaDokter(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    final View view = inflater.inflate(R.layout.activity_tanya_dokter, container, false);


    Button button = (Button)view.findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener()

    {   
        public void onClick(View v){
            switch(v.getId()){

            case R.id.button1:
            Intent intent1 = new Intent(view.getContext(), FormTanyaDokter.class);
                startActivity(intent1);
                break;

这段代码是成功点击一个按钮,但我想制作两个或更多按钮,所以我的问题是如何使这个代码可以处理两个或多个按钮点击。感谢

5 个答案:

答案 0 :(得分:1)

首先在xml文件中创建按钮:然后:有两种方法:

首先:

Button button = (Button)view.findViewById(R.id.button1);
Button button2 = (Button)view.findViewById(R.id.button2);
Button button3 = (Button)view.findViewById(R.id.button3);
button.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
 ----
}       

public void onClick(View v){
    switch(v.getId()){

    case R.id.button1:
        Intent intent1 = new Intent(view.getContext(), FormTanyaDokter.class);
        startActivity(intent1);
        break;

    case R.id.button2:
        //your action
        break;
    case R.id.button3:
        //your action
        break;
    }
}

第二

Button button = (Button)view.findViewById(R.id.button1);
Button button2 = (Button)view.findViewById(R.id.button2);
Button button3 = (Button)view.findViewById(R.id.button3);

button.setOnClickListener(new OnClickListener() {   

    public void onClick(View v){

        Intent intent1 = new Intent(view.getContext(), FormTanyaDokter.class);
        startActivity(intent1);

    }};

button2.setOnClickListener(new OnClickListener() {   

    public void onClick(View v) {
    }

});

button3.setOnClickListener(new OnClickListener() {   

    public void onClick(View v){
    }

});

答案 1 :(得分:0)

为了处理多个按钮点击事件,您必须这样做,

    public void onClick(View v){
        switch(v.getId()){

        case R.id.button1:
        Intent intent1 = new Intent(view.getContext(), FormTanyaDokter.class);
            startActivity(intent1);
            break;

       case R.id.button2:
       //your action
            break;
   }
 }

但我建议您在提出问题之前提交相应的文档,甚至可以查看Stackoverflow-Question

答案 2 :(得分:0)

单按钮

     Button cancel  = (Button) view.findViewById(R.id.button1);
     cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                       // your logic here....
                    }
                });

用于多个按钮。

  Button cancel1  = (Button) view.findViewById(R.id.button1);
  Button cancel2  = (Button) view.findViewById(R.id.button2);
  Button cancel3  = (Button) view.findViewById(R.id.button3);
  cancel1 .setOnClickListener(this);
  cancel2 .setOnClickListener(this);
  cancel2 .setOnClickListener(this);

OnClicked handle here

 @Override
  public void onClick(View v){
            switch(v.getId()){

            case R.id.button1:
            Intent intent1 = new Intent(view.getContext(), FormTanyaDokter.class);
                startActivity(intent1);
                break;
 case R.id.button2:
            Intent intent1 = new Intent(view.getContext(), FormTanyaDokter.class);
                startActivity(intent1);
                break;
 case R.id.button3:
            Intent intent1 = new Intent(view.getContext(), FormTanyaDokter.class);
                startActivity(intent1);
                break;

    }
 }

答案 3 :(得分:0)

您可以向xml布局添加更多按钮,然后通过此

处理所有点击
 Button button = (Button)view.findViewById(R.id.button1);
    Button button2 = (Button)view.findViewById(R.id.button2);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent1 = new Intent(view.getContext(), FormTanyaDokter.class);
            startActivity(intent1);
        }
    });

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent1 = new Intent(view.getContext(), SecondActivity.class);
            startActivity(intent1);
        }
    });

答案 4 :(得分:0)

您还可以将onClickListener设置为变量,并将所有按钮设置为该变量。 例如

View.onClickListener listener = new onClickListener()
{
    void onClick...
};
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
button3.setOnClickListener(listener);