android第六次课 UI2设计.docx

上传人:b****1 文档编号:1999795 上传时间:2023-05-02 格式:DOCX 页数:30 大小:54.73KB
下载 相关 举报
android第六次课 UI2设计.docx_第1页
第1页 / 共30页
android第六次课 UI2设计.docx_第2页
第2页 / 共30页
android第六次课 UI2设计.docx_第3页
第3页 / 共30页
android第六次课 UI2设计.docx_第4页
第4页 / 共30页
android第六次课 UI2设计.docx_第5页
第5页 / 共30页
android第六次课 UI2设计.docx_第6页
第6页 / 共30页
android第六次课 UI2设计.docx_第7页
第7页 / 共30页
android第六次课 UI2设计.docx_第8页
第8页 / 共30页
android第六次课 UI2设计.docx_第9页
第9页 / 共30页
android第六次课 UI2设计.docx_第10页
第10页 / 共30页
android第六次课 UI2设计.docx_第11页
第11页 / 共30页
android第六次课 UI2设计.docx_第12页
第12页 / 共30页
android第六次课 UI2设计.docx_第13页
第13页 / 共30页
android第六次课 UI2设计.docx_第14页
第14页 / 共30页
android第六次课 UI2设计.docx_第15页
第15页 / 共30页
android第六次课 UI2设计.docx_第16页
第16页 / 共30页
android第六次课 UI2设计.docx_第17页
第17页 / 共30页
android第六次课 UI2设计.docx_第18页
第18页 / 共30页
android第六次课 UI2设计.docx_第19页
第19页 / 共30页
android第六次课 UI2设计.docx_第20页
第20页 / 共30页
亲,该文档总共30页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

android第六次课 UI2设计.docx

《android第六次课 UI2设计.docx》由会员分享,可在线阅读,更多相关《android第六次课 UI2设计.docx(30页珍藏版)》请在冰点文库上搜索。

android第六次课 UI2设计.docx

android第六次课UI2设计

第五次课UI

(2)

5.单选按钮RadioButton

RadioButton的单选按钮;

RadioGroup是单选组合框,用于将RadioButton框起来;

在没有RadioGroup的情况下,RadioButton可以全部都选中;

当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个;

单选按钮的事件监听用setOnCheckedChangeListener来对单选按钮进行监听

Strings.xml

xmlversion="1.0"encoding="utf-8"?

>

哪个城市叫做山城?

单选按钮测试

杭州

成都

重庆

苏州

Main.xml

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

android:

orientation="vertical">

android:

id="@+id/textview1"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

text="@string/hello"

android:

textSize="20sp"/>

android:

id="@+id/radiogroup1"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_x="3px"

android:

orientation="vertical">

android:

id="@+id/radiobutton1"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="@string/radiobutton1"/>

android:

id="@+id/radiobutton2"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="@string/radiobutton2"/>

android:

id="@+id/radiobutton3"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="@string/radiobutton3"/>

android:

id="@+id/radiobutton4"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="@string/radiobutton4"/>

 

packagenynu.edu.sy;

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.Gravity;

importandroid.widget.RadioButton;

importandroid.widget.RadioGroup;

importandroid.widget.TextView;

importandroid.widget.Toast;

publicclassUisyActivityextendsActivity{

TextViewtextview;

RadioGroupradiogroup;

RadioButtonradio1,radio2,radio3,radio4;

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

textview=(TextView)findViewById(R.id.textview1);

radiogroup=(RadioGroup)findViewById(R.id.radiogroup1);

radio1=(RadioButton)findViewById(R.id.radiobutton1);

radio2=(RadioButton)findViewById(R.id.radiobutton2);

radio3=(RadioButton)findViewById(R.id.radiobutton3);

radio4=(RadioButton)findViewById(R.id.radiobutton4);

radiogroup

.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener(){

publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){

if(checkedId==radio3.getId())

{

DisplayToast("正确答案:

"+radio2.getText()

+",恭喜你,回答正确!

");

}else

{

DisplayToast("请注意,回答错误!

");

}

}

});

}

publicvoidDisplayToast(Stringstr)

{

Toasttoast=Toast.makeText(this,str,Toast.LENGTH_LONG);

toast.setGravity(Gravity.TOP,0,220);

toast.show();

}

}

6.CheckBox复选框

多选按钮CheckBox的可以实现多项选择,我们可以现在布局文件中定义多选按钮,然后对每一个多选按钮进行事件监听

setOnCheckedChangeListener,通过isChecked来判断选项是否被选中

xmlversion="1.0"encoding="utf-8"?

>

调查:

你喜欢Android的那款手机?

CheckBoxTest

HTCdesireHD

Googlenexusone

HTCdefy

摩托罗拉里程碑II

 

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

orientation="vertical"

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

>

android:

id="@+id/textview"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

text="@string/hello"

android:

textSize="20sp"

/>

android:

id="@+id/checkbox1"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

text="@string/checkbox1"

/>

android:

id="@+id/checkbox2"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

text="@string/checkbox2"

/>

android:

id="@+id/checkbox3"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

text="@string/checkbox3"

/>

android:

id="@+id/checkbox4"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

text="@string/checkbox4"

/>

android:

id="@+id/submit"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_gravity="right"

android:

text="提交信息"

/>

 

packagenynu.edu.sy;

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.Gravity;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.CompoundButton;

importandroid.widget.Button;

importandroid.widget.CheckBox;

importandroid.widget.RadioButton;

importandroid.widget.RadioGroup;

importandroid.widget.RadioGroup.OnCheckedChangeListener;

importandroid.widget.TextView;

importandroid.widget.Toast;

publicclassTextViewSyActivityextendsActivity{

/**Calledwhentheactivityisfirstcreated.*/

privateTextViewtextview;

privateButtonsubmit;

privateCheckBoxcheckbox1,checkbox2,checkbox3,checkbox4;

privateStrings=null;

 

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

textview=(TextView)findViewById(R.id.textview);

submit=(Button)findViewById(R.id.submit);

submit.setOnClickListener(newOnClickbt());

//取得每一个CheckBox对象

checkbox1=(CheckBox)findViewById(R.id.checkbox1);

checkbox2=(CheckBox)findViewById(R.id.checkbox2);

checkbox3=(CheckBox)findViewById(R.id.checkbox3);

checkbox4=(CheckBox)findViewById(R.id.checkbox4);

//为每一个选项设置监听

checkbox1.setOnCheckedChangeListener(newOnCheckchange());

checkbox2.setOnCheckedChangeListener(newOnCheckchange());

checkbox3.setOnCheckedChangeListener(newOnCheckchange());

checkbox4.setOnCheckedChangeListener(newOnCheckchange());

}

privateclassOnCheckchangeimplementsCheckBox.OnCheckedChangeListener{

publicvoidonCheckedChanged(CompoundButtonbuttonView,

booleanisChecked){

if(checkbox1.isChecked())

s=s+checkbox1.getText()+"";

elseif(checkbox2.isChecked())

s=s+checkbox2.getText()+"";

elseif(checkbox3.isChecked())

s=s+checkbox3.getText()+"";

elseif(checkbox4.isChecked())

s=s+checkbox4.getText()+"";

}

}

privateclassOnClickbtimplementsOnClickListener{

publicvoidonClick(Viewv){

DisplayToast(s);

}

}

publicvoidDisplayToast(Stringstr)

{

Toasttoast=Toast.makeText(this,str,Toast.LENGTH_SHORT);

//设置Toast的显示位置

toast.setGravity(Gravity.TOP,0,220);

//显示Toast

toast.show();

}

}

7.下拉列表框Spinner

1)采用xml建立下拉列表数据,setOnItemSelectedListener进行监听。

在values下建立intersts.xml,存放下拉列表的数据内容

xmlversion="1.0"encoding="utf-8"?

>

篮球

排球

足球

strings.xml

xmlversion="1.0"encoding="utf-8"?

>

HelloWorld,Activity01!

xh_spinner_test

您喜欢的球类运动是:

main.xml

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

orientation="vertical"

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

>

android:

text="@string/ys"

android:

id="@+id/TextView01"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

textSize="20sp"

/>

android:

id="@+id/instersting"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

entries="@array/interestsdata"

/>

Java程序

packagenynu.edu.sy;

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.widget.AdapterView;

importandroid.widget.AdapterView.OnItemSelectedListener;

importandroid.widget.ArrayAdapter;

importandroid.widget.Spinner;

importandroid.widget.TextView;

importandroid.widget.Toast;

publicclassUisyActivityextendsActivity{

privateSpinnerinterestspinner=null;

privateTextViewinfo=null;

protectedvoidonCreate(BundlesavedInstanceState){

//TODOAuto-generatedmethodstub

super.onCreate(savedInstanceState);

this.setContentView(R.layout.main);

interestspinner=(Spinner)this.findViewById(R.id.instersting);

info=(TextView)this.findViewById(R.id.TextView01);

interestspinner.setOnItemSelectedListener(newnItemSelectedListenerImp());

}

privateclassnItemSelectedListenerImpimplementsOnItemSelectedListener{

publicvoidonItemSelected(AdapterView

>arg0,Viewarg1,intarg2,

longarg3){

//TODOAuto-generatedmethodstub

Stringvalues=arg0.getItemAtPosition(arg2).toString();

Toast.makeText(UisyActivity.this,"您喜欢的球类运动是:

"+values,1).show();

}

publicvoidonNothingSelected(AdapterView

>arg0){

//TODOAuto-generatedmethodstub

}

}

}

2)首先需要在布局中定时Spinner组件,然后将可选内容通过ArrayAdapter和下拉列表连接起来,最后要获得用户选择的选项,设计事件监听器setOnItemSelectedListener并实现onItemSelected,从而获得用户所选择的内容,最后通过setVisibility方法设置当前的显示项

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

orientation="vertical"

android:

layout_width="fill_parent"

android:

layout_heig

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 高中教育 > 数学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2