我在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();
}
}
}main.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>但是我不能理解“私有OnClickListener clickFunction =新OnClickListener(){stmt;}”这个类的定义。我们曾经将类声明为私有类classname{stmt};,然后用来创建类的实例。但在上面的第一个声明中,他们创建了类的实例,然后定义。他们为什么要这样做。请帮我理解一下。
发布于 2012-01-26 19:33:55
OnClickListener是一个接口。当在该接口上使用new操作符时,实际上是为该接口提供了一个实现(一个匿名内部类)。类实现是匿名的,因为您没有在编译时为其提供名称,而将在运行时为其分配名称。您还可以为它提供一个显式的实现,作为实现OnClickListener接口的私有内部类。
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();
}
}
}发布于 2012-01-26 19:33:57
private OnClickListener clickFunction = new OnClickListener(){stmt; }但这不是私有类的定义..它是私有成员类的定义.
它就像这样:
//declaring member class
private OnClickListener clickFunction;
private OnClickListener clickFunctionMake(){
return new OnClickListener(){stmt; };
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//.....initializing member class
clickFunction=clickFunctionMake();
///....
}发布于 2012-01-26 19:43:24
private OnClickListener clickFunction = new OnClickListener(){stmt; }在上面的语句中,OnClickListener不是它的类,而是android.view.View类中的接口。我们不能通过使用关键字来实例化任何接口。
在您在这里发布的代码中,他通过实现View.OnClickListener接口来创建实例clickFunction,因此他还实现了onClick方法。
他为两个按钮使用了单实例clickFunction,以消除两个侦听器。
我希望你能理解这一点,这可能会对你有所帮助。
https://stackoverflow.com/questions/9017374
复制相似问题