Button,ImageButton,EditText三控件的应用。

本文介绍了一个简单的Android应用程序实例,展示了如何使用EditText进行文本输入、Button触发事件显示Toast消息以及ImageButton响应触摸事件改变背景图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要点:

1.获取控件函数  findViewById(R.id.xxxx);

2.获取图像资源 getResources.getDrawable(R.drawable.xxx)

3.背景图像,获取/设置EditText文本。



代码:

package com.loveyy;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);
		/*文本编辑器*/
		final EditText editText=(EditText)findViewById(R.id.txtEdit);
	
       /*按钮及按钮事件定义*/
		Button myBtn = (Button) findViewById(R.id.button1);
		myBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				// TODO Auto-generated method stub
				String StrTemp="Super";
				Toast.makeText(getApplicationContext(),StrTemp ,
						Toast.LENGTH_SHORT).show();
				
				editText.setText(StrTemp);
			}
		});
		
		/*图片按钮定义,设置背景图片及事件*/
		final ImageButton imgbtn=(ImageButton)findViewById(R.id.imageButton1);
		imgbtn.setOnTouchListener(new View.OnTouchListener() {
			public boolean onTouch(View view, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					imgbtn.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.press));

				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					imgbtn.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.nopress));
				}

				// TODO Auto-generated method stub
				return false;
			}
		});
	
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值