android学习笔记01

//MainActivity.java

package 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;
	@Override
	protected 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);*/
				
		
	}
	
	@Override
	public 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 {

		@Override
		public void onClick(View v) {
			count ++;
			textView.setText(count + "");
		}
		
	}
	
	//OnCheckedChangeListener 的使用方法<RadioGroup>
	class RadioGroupListener implements android.widget.RadioGroup.OnCheckedChangeListener {

		@Override
		public 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 {

		@Override
		public 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 {

		@Override
		public 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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值