<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/MyTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/MyButton"
android:text="点击产生随机数"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class AndroidButtonActivity extends Activity {
/** Called when the activity is first created. */
private TextView MyTextview = null;
private Button MyButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyTextview = (TextView)findViewById(R.id.MyTextView);
MyButton = (Button)findViewById(R.id.MyButton);
// 添加监听器
MyButton.setOnClickListener(new ButtonListener());
}
// 内部类实现
class ButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 产生随机数
String str = new Random().nextInt()+"";
MyTextview.setText(str);
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ImageButton
android:id="@+id/MyImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击切换图片" />
</LinearLayout>
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
public class AndroidImageButtonActivity extends Activity {
/** Called when the activity is first created. */
private ImageButton MyImageButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyImageButton = (ImageButton) findViewById(R.id.MyImageButton);
MyImageButton.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN) {
// 注意此处v和MyImageButton都可以
MyImageButton.setBackgroundResource(R.drawable.ic_launcher);
} else if(event.getAction() == MotionEvent.ACTION_UP) {
MyImageButton.setBackgroundResource(R.drawable.icon);
}
return true;
}
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<!-- android:inputType="number":输入类型为数字; -->
<!-- android:maxLength="2":输入最长为2; -->
<!-- android:password="true" :输入的形式为密码 -->
<!-- android:numeric="integer":输入整数 -->
<EditText
android:id="@+id/et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="@string/hello" />
</LinearLayout>
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
public class AndroidEditTextActivity extends Activity {
/** Called when the activity is first created. */
private EditText et = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText) findViewById(R.id.et);
}
}