您好,登錄后才能下訂單哦!
布局:layout/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">
<TextView
android:id="@+id/tv"
android:text="點(diǎn)擊變字體顏色"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
/>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbMale"
android:text="男"
android:checked="true"
/>
<RadioButton
android:id="@+id/rbfaMale"
android:text="女"/>
<CheckBox
android:id="@+id/cbMale"
android:text="變男"/>
<CheckBox
android:id="@+id/cbfaMale"
android:text="變女"/>
</RadioGroup>
</LinearLayout>
java類:
package com.example.day03_01;
import android.support.v7.app.ActionBarActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private RadioButton rbMale ;//初始化-----男
private RadioButton rbfaMale ;//初始化------女
private RadioGroup rg;
private TextView tv;
private ImageView iv;
private CheckBox cbMale;
private CheckBox cbfaMale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
setListener();
}
private void setListener() {
// TODO Auto-generated method stub
cbMale.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
rbMale.setChecked(((CheckBox)view).isChecked());
rbfaMale.setChecked(!((CheckBox)view).isChecked());
}
});
cbfaMale.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
rbMale.setChecked(!((CheckBox)view).isChecked());
rbfaMale.setChecked(((CheckBox)view).isChecked());
}
});
tv.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
TextView tv = (TextView)view;
tv.setTextColor(Color.RED);
}
});
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
ImageView iv = (ImageView)view;
iv.setImageResource(R.drawable.p01);
}
});
}
private void init(){//初始化 ----男,女
rbMale = (RadioButton) findViewById(R.id.rbMale);
rbfaMale = (RadioButton) findViewById(R.id.rbfaMale);
rg = (RadioGroup)findViewById(R.id.rg);
tv = (TextView)findViewById(R.id.tv);
iv = (ImageView)findViewById(R.id.iv);
cbMale = (CheckBox)findViewById(R.id.cbMale);
cbfaMale = (CheckBox)findViewById(R.id.cbfaMale);
}
}
效果:
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。