setTitle(\开始游戏!\ break; case ITEM2: setTitle(\退出!\ break; } return true; }
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.add(0, ITME3, 0, \红色背景\ menu.add(0, ITME4, 0, \绿色背景\ menu.add(0, ITME5, 0, \白色背景\ } public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case ITME3: myTV.setBackgroundColor(Color.RED); break; case ITME4: myTV.setBackgroundColor(Color.GREEN); break; case ITME5: myTV.setBackgroundColor(Color.WHITE); break; } return true; } }
3.D.5 演示提示对话框的使用 AlertDialogActivity.java
public class AlertDialogActivity extends Activity { private TextView myTV; private Button myBtn; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myTV = (TextView) findViewById(R.id.TextView01); myBtn = (Button) findViewById(R.id.Button01); final AlertDialog.Builder builder = new AlertDialog.Builder(this); myBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { builder.setMessage(\真的要删除该记录吗?\
.setPositiveButton(\是\ new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { myTV.setText(\删除成功!\ } }) .setNegativeButton(\否\ new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { myTV.setText(\取消删除!\ } }); AlertDialog ad = builder.create(); ad.show(); } }); } }
3.D.6 Toast的创建及显示 ToastActivity.java
public class ToastActivity extends Activity { private Button b1, b2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); b1 = (Button) findViewById(R.id.Button01); b2 = (Button) findViewById(R.id.Button02); b1.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast t1 = Toast.makeText(getApplicationContext(), \我多显示一会儿!\ Toast.LENGTH_LONG); t1.show(); } }); b2.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast t2 = Toast.makeText(getApplicationContext(), \我少显示一会儿!\ Toast.LENGTH_SHORT); t2.show(); }
}
}
});
3.D.7使用Toast显示注册信息
RegActivity.Java
public class RegActivity extends Activity { // 声明组件 private Button register, cancel; private ToggleButton marriged; private RadioButton male, female; private EditText username, password; private Spinner position; private CheckBox reading, swimming; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); username = (EditText) findViewById(R.id.username); password = (EditText) findViewById(R.id.password); male = (RadioButton) findViewById(R.id.male); female = (RadioButton) findViewById(R.id.female); reading = (CheckBox) findViewById(R.id.reading); swimming = (CheckBox) findViewById(R.id.swimming); marriged = (ToggleButton) findViewById(R.id.marriged); position = (Spinner) findViewById(R.id.position); String[] strs = { \ ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, strs); position.setAdapter(aa); register = (Button) findViewById(R.id.register); cancel = (Button) findViewById(R.id.cancel); register.setOnClickListener(new OnClickListener() { public void onClick(View v){
Toast.makeText(getApplicationContext(), \ Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), \ Toast.LENGTH_LONG).show(); if (male.isChecked()) {
Toast.makeText(getApplicationContext(), \ } else {
Toast.makeText(getApplicationContext(), \ } String temp = \ if (reading.isChecked()) { temp += \ } if (swimming.isChecked()) { temp += \ }
Toast.makeText(getApplicationContext(), temp, Toast.LENGTH_LONG).show(); if (marriged.isChecked()) {
Toast.makeText(getApplicationContext(), \ } else { Log.i(\
Toast.makeText(getApplicationContext(), \ }
Toast.makeText(getApplicationContext(), \ Toast.LENGTH_LONG).show(); } }); } }
第四章代码
① 通过Intent实现多Activity的Android应用的启动
public class Activity1 extends Activity {
public void onCreate(Bundle savedInstanceState) { RadioGroup RG_OS;
RadioButton RG_OS_RB1, RG_OS_RB2, RG_OS_RB3; Button button_submit, button_back; super.onCreate(savedInstanceState); setContentView(R.layout.activity1);
RG_OS = (RadioGroup) findViewById(R.id.RG_OS);
RG_OS_RB1 = (RadioButton) findViewById(R.id.RG_OS_RB1); RG_OS_RB2 = (RadioButton) findViewById(R.id.RG_OS_RB2); RG_OS_RB3 = (RadioButton) findViewById(R.id.RG_OS_RB3); button_submit = (Button) findViewById(R.id.button_submit);
相关推荐: