简体   繁体   English

Android Button侦听器代码最小化

[英]Android Button Listener Code Minimize Required

i have started development on android application and somehow i have managed to make a button and text on activity and on button click i will change the text on TextView. 我已经开始在android应用程序上进行开发,并且以某种方式设法在活动上创建了一个按钮和文本,并在单击按钮时我将更改TextView上的文本。 now the problem is Android button listener code is too long and i want to minimize this code? 现在的问题是Android按钮侦听器代码太长,我想最小化此代码? please help is there any way to Minimize the Code 请帮助有什么办法可以最小化代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button ArsalButton = (Button)findViewById(R.id.ArsalButton);
    final TextView ArsalText = (TextView)findViewById(R.id.ArsalText);
    ArsalButton.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View v){
                    ArsalText.setText("You Done it!");
                }
            }
    );

}

There is no need to minimize this code until you have this single button. 在您拥有单个按钮之前,无需最小化此代码。 But if you have multiple buttons on your screen and you want to set click listener for all of them then this is not a good approach. 但是,如果您的屏幕上有多个按钮,并且想要为所有按钮设置点击监听器,那么这不是一个好方法。 In that case your code should be like: 在这种情况下,您的代码应类似于:

  public class YourActivity extends Activity implements Button.OnClickListener {

         protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         Button ArsalButton = (Button)findViewById(R.id.ArsalButton);
         final TextView ArsalText = (TextView)findViewById(R.id.ArsalText);
         ArsalButton.setOnClickListener(this);

         }

         @Override
         public void onClick(View v) {
            // get view id and perform operation based on id.
         }
  } 

In your case AFAIK for clicklistner whatever the code like adding listner & handling click is required that we need to do, and in that case no need to minimize As per my opinion in your code optimize is possible only in case if you are not using Button Instance anywhere thn give direct click event to View instance as setOnClickListner is view method 在您的情况下,我们需要使用AFAIK for clicklistner进行任何操作,例如添加列表器和处理单击等代码,在这种情况下,无需最小化。根据我的观点,只有在您不使用Button的情况下,优化代码才是可能的setOnClickListner是view方法的任何实例都可以向View实例提供直接单击事件

Below three different way you can give it you can use which ever you find minimized/optimized. 您可以通过以下三种不同的方式给予它,无论使用哪种方法,都可以将其最小化/优化。

// if button or textview instance is not required any where outside than
        // Button ArsalButton = (Button)findViewById(R.id.ArsalButton);
        //final TextView ArsalText = (TextView)findViewById(R.id.ArsalText);
        findViewById(R.id.ArsalButton).setOnClickListener(
                new Button.OnClickListener() {
                    public void onClick(View v) {
                        ((TextView) findViewById(R.id.ArsalText)).ArsalText.setText("You Done it!");

                    }
                }
        );

Second way is like implement onlclick listner interface & handle click 第二种方式就像实现onlclick listner界面并处理click

public class YourActivity extends Activity implements Button.OnClickListener {

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            findViewById(R.id.ArsalButton).setOnClickListener(this);
            // if button or textview instance is not required any where outside than
            // Button ArsalButton = (Button)findViewById(R.id.ArsalButton);
            //final TextView ArsalText = (TextView)findViewById(R.id.ArsalText);
            findViewById(R.id.ArsalButton).setOnClickListener(
                    new Button.OnClickListener() {
                        public void onClick(View v) {


                        }
                    }
            );

        }

        @Override
        public void onClick(View v) {
            ((TextView) findViewById(R.id.ArsalText)).ArsalText.setText("You Done it!");
        }
    }

Another ways is like give click event in XML Button 另一种方式就像XML Button中的Give click事件

public class YourActivity extends Activity {

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }

        public void onArsalButtonClick(View view) {
            ((TextView) findViewById(R.id.ArsalText)).ArsalText.setText("You Done it!");
        }

    }

  <Button
        android:id="@+id/ArsalButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onArsalButtonClick"
        android:text="ArsalButton" />

You don't need to minimize it. 您无需将其最小化。 However, you have alternatives to it: 但是,您还有其他选择:

To set the listener in the xml: 要在xml中设置侦听器,请执行以下操作:

<Button
   android:id="@+id/myButton"
   ...
   android:onclick="myButtonClick"
/>

Then you need to declare myButtonClick method in your activity. 然后,您需要在活动中声明myButtonClick方法。

public void myButtonClick(View view){
    ArsalText.setText("You done it");
}

Or to implement OnClickListener in your activity: 或在您的活动中实现OnClickListener:

public class MyActivity extends Activity implements View.OnClickListener{  

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      ...
      ArsalButton.setOnClickListener(this);   
   }

    @Override
    public void onClick(View view){
        switch (view.getId()){
            case R.id.myButton:
                ArsalText.setText("You Done It");
                break;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM