匿名内部类的知识

package sun.sms;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SmsActivity extends Activity {
    /** Called when the activity is first created. */
	
	private Button sendButton ; 
	private EditText editText1 ; 
	private EditText editText2; 
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button sendButton = (Button)this.findViewById(R.id.sendbutton);
        sendButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				EditText editText1 = (EditText)findViewById(R.id.phonetext);
				EditText editText2 = (EditText)findViewById(R.id.contenttext);
		        
				String mobile = editText1.getText().toString();
				String content = editText2.getText().toString();
				
				SmsManager smsManager = SmsManager.getDefault();
				List<String> texts =  smsManager.divideMessage(content);
				for(String text:texts){
					smsManager.sendTextMessage(mobile, null, text, null, null);
				}
				Toast.makeText(SmsActivity.this, "发送完成", 1).show();
			}
		});
        
    }
}

  

这些都是java 的基础知识,之前不很明白EditText editText1 = (EditText)findViewById(R.id.phonetext);
findViewById是外部类SmsActivity 的方法,为什么这里可以直接用findViewById,原因是因为匿名内部类不存在findViewById方法,所以他会寻找外部类SmsActivity 的findViewById。由此可见,当匿名内部类存在findViewById同名方法时,就不能省略SmsActivity .this .findViewById的引用了.

你可能感兴趣的:(匿名内部类)