TextView,Menu,Button,AlertDialog,EditText,Toast,ImageView的用法

 

package com.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class TestActivity extends Activity {
	private Button button1;
	private Button button2;
	private TextView textView;
	private EditText editText;
	private ImageButton imageButton;
	

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

		button1 = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.button2);
		textView = (TextView) findViewById(R.id.textView1);
		editText = (EditText) findViewById(R.id.editText1);
		imageButton =(ImageButton) findViewById(R.id.imageButton1);
		
		button1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				textView.setText("Hi, everyone");
				Toast.makeText(TestActivity.this, editText.getText(), Toast.LENGTH_LONG).show();
			}
		});
		button2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				textView.setText("Hello, fuxin");
				imageButton.setImageResource(R.drawable.i03);
			}
		});
		editText.setOnKeyListener(new View.OnKeyListener() {
			
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				textView.setText(editText.getText());
				return false;
			}
		});
		
		imageButton.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if(hasFocus == true){
					imageButton.setImageResource(R.drawable.i02);
				}else{
					imageButton.setImageResource(R.drawable.i03);
				}				
			}
		});
		imageButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				imageButton.setImageResource(R.drawable.i02);
			}
		});
	}

		
	// 创建菜单Menu
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 0, 0, "关于");
		menu.add(0, 1, 1, "离开");
		return super.onCreateOptionsMenu(menu);
	}

	// 识别按下哪个菜单
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case 0:
			openOptionsDialog();
			break;
		case 1:
			exitOptionDialog();
			break;
		}
		return true;
	}

	// 点击菜单"关于"
	private void openOptionsDialog() {
		new AlertDialog.Builder(TestActivity.this).setTitle("关于")
				.setMessage("AlertDialog练习")
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
					}
				}).show();
	}

	private void exitOptionDialog() {
		new AlertDialog.Builder(TestActivity.this).setTitle(R.string.exit)
				.setMessage("确认要离开吗?")
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
					}
				})
				.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
 					public void onClick(DialogInterface dialog,	int which) {
					// TODO Auto-generated method stub
 						finish();
					}
		}).show();
	}
}

/p

你可能感兴趣的:(TextView,Menu,Button,AlertDialog,EditText,Toast,ImageView的用法)