要点:
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();
}
}