gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:09:32 29 4
gpt4 key购买 nike

我正在尝试 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com