android学习笔记01

来源:互联网 发布:知乎 图片浏览软件 编辑:程序博客网 时间:2024/05/29 17:51
//MainActivity.javapackage zmj.Review_01;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.TextView;public class MainActivity extends Activity {private TextView textView;private Button button;private RadioGroup radioGroup;private RadioButton maleButton;private RadioButton femaleButton;//多选按钮/*private CheckBox eat;private CheckBox sleep;private CheckBox dota;*/int count = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView = (TextView)findViewById(R.id.textView);button = (Button)findViewById(R.id.button);radioGroup = (RadioGroup)findViewById(R.id.radioGroupId);maleButton = (RadioButton)findViewById(R.id.maleButton);femaleButton = (RadioButton)findViewById(R.id.femaleButton);/*eat = (CheckBox)findViewById(R.id.eat);sleep = (CheckBox)findViewById(R.id.sleep);dota = (CheckBox)findViewById(R.id.dota);*/textView.setText("0");textView.setBackgroundColor(Color.BLUE);ButtonListener buttonListener = new ButtonListener();button.setOnClickListener(buttonListener);RadioGroupListener radioGroupListener = new RadioGroupListener();radioGroup.setOnCheckedChangeListener(radioGroupListener);/*CheckBoxListener1 checkBoxListener1 = new CheckBoxListener1();eat.setOnCheckedChangeListener(checkBoxListener1);sleep.setOnCheckedChangeListener(checkBoxListener1);dota.setOnCheckedChangeListener(checkBoxListener1);*//*CheckBoxListener checkBoxListener = new CheckBoxListener();eat.setOnClickListener(checkBoxListener);sleep.setOnClickListener(checkBoxListener);dota.setOnClickListener(checkBoxListener);*/}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}class ButtonListener implements OnClickListener {@Overridepublic void onClick(View v) {count ++;textView.setText(count + "");}}//OnCheckedChangeListener 的使用方法<RadioGroup>class RadioGroupListener implements android.widget.RadioGroup.OnCheckedChangeListener {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {if(checkedId == maleButton.getId()) {System.out.println("male");}else if(checkedId == femaleButton.getId()) {System.out.println("female");}}}//OnCheckedChangeListener的使用方法<CompoundButton>/*class CheckBoxListener1 implements OnCheckedChangeListener {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {String info= null;if(buttonView.getId() == R.id.eat) {info = "吃饭";}else if (buttonView.getId() == R.id.sleep) {info = "睡觉";}else if(buttonView.getId() == R.id.dota){info = "Dota";}if(isChecked) {System.out.println("isChecked");}else {System.out.println("isUnChecked");}System.out.println(info);}*///OnClickListener的使用方法/*class CheckBoxListener implements OnClickListener {@Overridepublic void onClick(View v) {CheckBox box = (CheckBox)v;String info = null;if(v.getId() == R.id.eat) {info = "吃饭";} else if(v.getId() == R.id.sleep) {info = "睡觉";} else {info = "Dota";}if(box.isChecked()) {System.out.println("isChecked");} else {System.out.println("isUnChecked");}System.out.println(v.getId());System.out.println(info);}}*/}//activity_main.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity" >    <TextView        android:id="@+id/textView"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textSize="50px"        android:background="#00ff00"        android:layout_centerVertical="true"        android:layout_centerHorizontal="true"        android:text="Hello Java!" />        <Button         android:id="@+id/button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="button"/>        <CheckBox         android:id="@+id/eat"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="吃饭"/>    <CheckBox         android:id="@+id/sleep"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="睡觉"/>    <CheckBox         android:id="@+id/dota"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Dota"/>        <RadioGroup         android:id="@+id/radioGroupId"        android:layout_width="wrap_content"        android:layout_height="wrap_content">                <RadioButton             android:id="@+id/maleButton"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="male"/>                <RadioButton             android:id="@+id/femaleButton"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="female"/>    </RadioGroup></LinearLayout>

原创粉丝点击