如何使点击监听器成为活动的属性?

时间:2015-06-25 21:18:40

标签: java android onclicklistener

我正在尝试将点击事件作为我的活动的属性。我不确定这是否是最好的方法,但作为Android的新手,我借用了C#或JavaScript中的知识。这就是我想要做的事情:

public class MainActivity extends Activity {

    TextView topTextView;
    Button counterButton;

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

        // Init view elements
        topTextView = (TextView) findViewById(R.id.textView1);
        counterButton = (Button)findViewById(R.id.button1);

        // Init element events
        counterButton.setOnClickListener(onCounterButtonClick);
    }

    private OnClickListener onCounterButtonClick(final MainActivity activity) {
        return new OnClickListener() {

            @Override
            public void onClick(View view) {
                Toast toast = Toast.makeText(activity, 
                    "Button was clicked", Toast.LENGTH_LONG);
                toast.show();

                topTextView.setText("Updated from button");
            }

        };
    }
}

有更好的方法吗?

5 个答案:

答案 0 :(得分:2)

您可以将侦听器声明为该类的字段。

private OnClickListener onCounterButtonClick  = new OnClickListener(){
        @Override
        public void onClick(View view) {
            Toast toast = Toast.makeText(MainActivity.this, "Button was clicked", Toast.LENGTH_LONG);
            toast.show();
        }
    };
}

并将其用作参数。

答案 1 :(得分:1)

如果这是你想要的,我现在不知道。但Android有其他方法来定义Button中的操作,例如: 在activity.xml中定义Button属性onClick=myFunction,然后在Activity.java中编写如下方法:

activity.xml

....
<Button
    android:id="@+id/Button_1"
    android:onClick="myFunction"
    android:text="@string/Button_1" />
....

Activity.java

public void myFunction(View v) {
    Toast toast = Toast.makeText(this, "Button was clicked", Toast.LENGTH_LONG).show();
}
祝你好运!

答案 2 :(得分:1)

你也可以使用它,

public class MainActivity extends Activity implements OnClickListener{

    TextView topTextView;
    Button counterButton;

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

        // Init view elements
        topTextView = (TextView) findViewById(R.id.textView1);
        counterButton = (Button)findViewById(R.id.button1);

        // Init element events
        counterButton.setOnClickListener(this);
    }

            @Override
            public void onClick(View view) {
               if(view.getId == R.id.button1){
                Toast toast = Toast.makeText(activity, 
                    "Button was clicked", Toast.LENGTH_LONG);
                toast.show();

                topTextView.setText("Updated from button");
                }
           }
}

答案 3 :(得分:0)

您可以使用View.OnClickListener()匿名内部类。

counterButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Toast toast = Toast.makeText(activity, 
                "Button was clicked", Toast.LENGTH_LONG);
            toast.show();
        }
    });

答案 4 :(得分:0)

onClickListener

您也可以直接设置getApplicationContext(),而不是传递参数的活动,您可以使用 #!/bin/bash/ function my_function() { MYPARSER="$1" python - <<END <<Some Python Code>> class MyParser(OptionParser): def format_epilog(self, formatter): return self.epilog parser=MyParser(version=VER, usage=USAGE, epilog=DESC) parser.add_option("-s", "--Startdir", dest="StartDir", metavar="StartDir" ) parser.add_option("-r", "--report", dest="ReportDir", metavar="ReportDir" ) <<More Python Code>> END } foo="-s /mnt/folder -r /storagefolder/" my_function "$foo" 函数来获取上下文。