java - private OnClickListener clickFunction = new OnClickListener(){stmt; }

标签 java android

我正在尝试 android 上的示例应用程序。我可以获得输出。

studentFormsActiviyt.java

package com.example.android.accelerometerplay;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import android.content.Context;

public class StudentFormsActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // To specify the actions of the Buttons
        Button accept = (Button) findViewById(R.id.myButton1);
        Button reject = (Button) findViewById(R.id.myButton2);

        accept.setOnClickListener(clickFunction);
        reject.setOnClickListener(clickFunction);
    }

    private OnClickListener clickFunction = new OnClickListener(){
        public void onClick(View v){
            Context context = getApplicationContext();
            CharSequence text;

            switch(v.getId()){
               case R.id.myButton1: text="accept was pushed"; 
                                    break;
               case R.id.myButton2: text="reject was pushed"; 
                                    break;
               default: text="We didn't know what was pressed :(";  
            }

            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context,text,duration);
            toast.show();

        }
    }
 }

主.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:id="@+id/myLabel"
    android:text="Name of the student:" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
<EditText
    android:id="@+id/myText"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_below="@id/myLabel" 
    />
<Button
    android:id="@+id/myButton1"
    android:text="Accept:" 
    android:textColor="#00FF00"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_below="@id/myText"
    android:layout_alignParentRight="true"
    />
<Button
    android:id="@+id/myButton2"
    android:text="Reject:" 
    android:textColor="#FF0000"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/myButton1"
    android:layout_alignTop="@id/myButton1"
    />
</RelativeLayout>

但我无法理解类“private OnClickListener clickFunction = new OnClickListener(){stmt; }”的定义。我们曾经将类声明为 private class classname{stmt};然后用于创建类的实例。但是在上面的第一个声明中,他们正在创建类的实例,然后他们正在定义。 他们为什么这样做。请帮助我理解。

最佳答案

OnClickListener 是一个接口(interface)。当在该接口(interface)上使用 new 运算符时,您实际上是在为该接口(interface)(匿名内部类)提供实现。类实现是匿名的,因为您没有在编译时为其提供名称,而是在运行时为其分配名称。您还可以为其提供显式实现,作为实现 OnClickListener 接口(interface)的私有(private)内部类。

    package com.example.android.accelerometerplay;

    import android.app.Activity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.os.Bundle;
    import android.widget.Button;
    import android.widget.Toast;
    import android.content.Context;

    public class StudentFormsActivity extends Activity {
    /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // To specify the actions of the Buttons
    Button accept = (Button) findViewById(R.id.myButton1);
    Button reject = (Button) findViewById(R.id.myButton2);

    accept.setOnClickListener(clickFunction);
    reject.setOnClickListener(clickFunction);
}

private OnClickListener clickFunction = new OnClickClass();

private class OnClickClass implements OnClickListener{
    public void onClick(View v){
        Context context = getApplicationContext();
        CharSequence text;

        switch(v.getId()){
           case R.id.myButton1: text="accept was pushed"; 
                                break;
           case R.id.myButton2: text="reject was pushed"; 
                                break;
           default: text="We didn't know what was pressed :(";  
        }

        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context,text,duration);
        toast.show();

    }
 }
}

关于java - private OnClickListener clickFunction = new OnClickListener(){stmt; },我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9017374/

相关文章:

android - ISO 14443 A 类卡读/写使用 Android

java - 尽管使用 servletContext(),getResourceAsStream 返回 null

java - 将连接数据转换为从 oracle 过程返回的 Bean

java - Spring Boot Lombok 可嵌入主键日期

java - Java中如何设置图标显示在最右边

java - 在父 View 中多次动态包含 XML 相对布局

java - Android 将日期格式从字符串更改为日期和时间分隔

java - 如何创建嵌套 json 作为 HttpPost 实体

java - 如何从 android 市场加载 apk 依赖项?

java - 尝试调用虚方法 'java.lang.Object android.content.Context.getSystemService(java.lang.String)