Android基本控件及表单三大控件

(一)、需要掌握的n个UI控件、组件名称:
(二)、基本控件:—— TextView:

1、andorid:text 2、 android:textColor 3、 android:textSize  4、andorid:height    5、 android:width             6、 android:inputType      7、 android:singleLine      8、android:gravity     9、android:drawableLeft     10、android:drawableRight     11、android:drawableTop     12、android:drawableBottom    13、android:autoLink  (web / email / phone / map / all / none)  14、android:hint    


二、基本控件:—— EditText(一)、 EditText 类结构:java.lang.Object   ↳  android.view.View      ↳  android.widget.TextView         ↳  android.widget.EditText
备注:EditView类继承自TextView类,EditView与TextView最大的不同就是用户可以对EditView控件进行编辑,同时还可以为EditView控件设置监听器,用来判断用户的输入是否合法。
(二)、EditView常用属性:
(三)、android:inputType的可选项:

  1. android:inputType="textPersonName" 
  2. android:inputType="textPassword" 
  3. android:inputType="numberPassword" 
  4. android:inputType="textEmailAddress" 
  5. android:inputType="phone" 
  6. android:inputType="textPostalAddress" 
  7. android:inputType="time" 
  8. android:inputType="date" 
  9. android:inputType="number" 

(四)、EditText常用方法:
    1、setText ()     2、getText ()


三、基本控件:—— ImageView: (一)、类结构:

java.lang.Object    ↳ android.view.View     ↳ android.widget.ImageView

 

(二)、 ImageView 常用属性: 1、andorid:src   设置图片来源。属性值为android:src="@drawable/图片名称"2、android:adjustViewBounds    用于设置 ImageView 是否调整自己的边界,来保持所显示图片的长宽比例。属性值为true或false3、 android:maxHeight       设置 ImageView 的最大高度。需要先设置android:adjustViewBounds为true,否则不起作用。4、andorid:maxWidth         设置 ImageView 的最大宽度。需要先设置android:adjustViewBounds为true,否则不起作用。5、 android:scaleType           设置所显示的图片如何缩放或移动,以适应ImageView的大小。可选项:fitCenter、fitStart 、 fitEnd、 fitXY 、 center、centerCrop、centerInside、matrix

  • matrix  :保持原图大小、从左上角的点开始,以矩阵形式绘图。 MATRIX 用图片的矩阵从左向开始来画,不做任何拉伸。如果一个100*100的ImageView,它的src是10*10的小图,则图显示在左上角,如果scr是200*200的大图,则截取它左上的100*100做显示。
  • fitXY :把图片按照指定的大小在View中显示,拉伸显示图片,不保持原比例,填满View. FIT_XY 不保持图片横宽比,把图片的宽和高分别拉伸或缩放至ImageView的大小。如果是一个100*100的ImageView,它的src是20*10,则直接把它拉伸为100*100后显示,如果src是200*100,则把它缩放为100*100后显示,此种方法容易导致图片变形。

  • fitStart :把图片按比例扩大(缩小)到View的宽度,显示在View的上部分位置 。FIT_START 与 FIT_CENTER 缩放拉伸原则一样,区别是处理过的图片居左显示。
  • fitCenter :把图片按比例扩大(缩小)到View的宽度,居中显示 。FIT_CENTER 保持横宽比,对图片进行拉伸或缩放,原则是:
    • 1)图片能完整显示;
    • 2)图片宽或高至少有一样与ImageView的相同;
    • 3)处理过的图片居中显示。
    • 如果是一个100*100的ImageView,它的src是20*10,先把图片等比放大到100*50,然后再居中显示。它的src是100*200,会先把图片等比缩放到50*100,然后再居中显示。
  • fitEnd :把图片按比例扩大(缩小)到View的宽度,显示在View的下部分位置 。FIT_END 与 FIT_CENTER缩放拉伸原则一样,区别是处理过的图片居右显示。

  • Center : 以原图的几何中心点和ImagView的几何中心点为基准,按图片的原来size居中显示,不缩放, 当图片长/宽超过View的长/宽,则截取图片的居中部分显示ImageView的size. 当图片小于View 的长宽时,只显示图片的size,不剪裁。 CENTER 不做任何拉伸,以居中的方式显示图片。如果是一个100*100的ImageView,它的src是10*10的小图,则图显示在的中央,如果src是200*200的大图,截取中间的100*100用来显示。
  • centerCrop :以原图的几何中心点和ImagView的几何中心点为基准,按比例扩大(图片小于View的宽时)图片的size。 居中显示,使得图片长 (宽)等于或大于View的长(宽),并按View的大小截取图片。 当原图的size大于ImageView时,按比例缩小图片,使得长宽中有一向等于ImageView,另一向大于ImageView。 CENTER_CROP 保持图片横宽比,以图片中心为基点进行拉伸显示,拉伸的原则是填充满整个ImageView。如果是一个100*100的ImageView,它的src是20*10,则它会被等比拉伸成200*100,然后再截取其中央的100*100显示。如果src是200*300的,直接截取中央的100*100显示,不做任何拉伸或缩放。
  • centerInside :以原图的几何中心点和ImagView的几何中心点为基准,将图片的内容完整居中显示, 通过按比例缩小原来的size使得图片长(宽)等于或小于ImageView的长(宽)。CENTER_INSIDE 保持图片横宽比,以图片中心为基点进行缩放显示,缩放的原则是显示完整个图片。如果是一个100*100的ImageView,它的src是20*10,它直接居中显示。如果src是200*100的,先把图片等比缩小到100*50,然后再居中显示。


        一般情况下,设置为centerCrop能获得较好的适配效果。

(三)、ImageView常用方法:

1、setImageBitmap() 2、setImageDrawable() 3、setImageResource()
 

(四)、实现图片变换的核心代码:



publicclass MainActivity extends Activity {

private ImageView imageView_main_show;

// 定义一个数组,用来存放所有图片的id

privateint[] imgIds = { R.drawable.img001, R.drawable.img012,

R.drawable.img017, R.drawable.img021, R.drawable.img030,

R.drawable.img031, R.drawable.img033, R.drawable.img038,

R.drawable.img039 };

privateintindex = 0;

@Override

protectedvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

imageView_main_show = (ImageView) findViewById(R.id.imageView_main_show);

}

publicvoid clickButton(View view) {

switch (view.getId()) {

case R.id.button_main_previous:

index--;

break;

case R.id.button_main_next:

index++;

break;

}

if (index < 0) {

index = imgIds.length - 1;

}

if (index > imgIds.length - 1) {

index = 0;

}

imageView_main_show.setImageResource(imgIds[index]);

}

@Override

publicboolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

returntrue;

}

}


 

四、基本控件:—— RadioButton及RadioGroup(一)、类结构介绍:java.lang.Object   ↳ android.view.View    ↳ android.view.ViewGroup      ↳ android.widget.LinearLayout        ↳ android.widget.RadioGroup
java.lang.Object   ↳ android.view.View    ↳ android.widget.TextView      ↳ android.widget.Button        ↳ android.widget.CompoundButton          ↳ android.widget.RadioButton
RadioButton继承于Button,所以具有普通按钮的各种属性,但是与普通按钮不同的是,RadioButton提供了可选中的功能。在使用RadioButton的时候,要使用RadioGroup来包围起这些RadioButton。【备注:】RadioGroup是LinearLayout的子类,所以RadioGroup本质上是一个存放RadioButton的布局容器。需要记住的是:默认的LinearLayout布局的Orientation属性是水平的,而默认的RadioGroup的Orientation属性是垂直的。
(二)、重点记忆的类方法:1、RadioGroup类中的getCheckedRadioButtonId()

(三)、核心代码:



// A.、UI的代码:

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

    

        android:id="@+id/button_main_submit"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="clickButton"

        android:text="提交" />

    

        android:id="@+id/radioGroup_main_sex"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" >

        

            android:id="@+id/radioButton_main_female"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="女" />

        

            android:id="@+id/radioButton_main_male"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:checked="true"

            android:text="男" />

    


 // B、java代码:

public class MainActivity extends Activity {

private RadioGroup radioGroup_main_sex;

private Button button_main_submit;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

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

radioGroup_main_sex = (RadioGroup) findViewById(R.id.radioGroup_main_sex);

button_main_submit.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// 获取勾选项的id

int id = radioGroup_main_sex.getCheckedRadioButtonId();

// 通过id找到被勾选项的控件

RadioButton radioButton = (RadioButton) findViewById(id);

// 通过控件的getText()方法找到该控件的text属性的值

String result = radioButton.getText().toString();

Toast.makeText(MainActivity.this, "您选择了:" + result,

Toast.LENGTH_LONG).show();

}

});

radioGroup_main_sex

.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

RadioButton radioButton = (RadioButton) findViewById(checkedId);

String result = radioButton.getText().toString();

Toast.makeText(MainActivity.this, "您选择了:" + result,

Toast.LENGTH_LONG).show();

}

});

}

@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;

}

}


  
五、CheckBox:(一)、 类结构介绍:java.lang.Object   ↳ android.view.View    ↳ android.widget.TextView      ↳ android.widget.Button        ↳ android.widget.CompoundButton          ↳ android.widget.CheckBox
CheckBox继承于Button,所以具有普通按钮的各种属性,但是与普通按钮不同的是, CheckBox 提供了可选中的功能。【备注:】CheckBox有一个父类是CompoundButton,所以在使用监听器的时候要注意跟单选项进行区别。

(二)、核心代码:



  A.、UI的代码:

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

    

        android:id="@+id/checkBox_main_hobby1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="游泳" />

    

        android:id="@+id/checkBox_main_hobby2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="上网" />

    

        android:id="@+id/checkBox_main_hobby3"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="音乐" />

    

        android:id="@+id/checkBox_main_hobby4"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="睡觉" />

    

    

        android:id="@+id/checkBox_main_selectall"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="全选" />

    

        android:id="@+id/button_main_submit"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="提交" />

    B、java代码:

public class MainActivity extends Activity {

private CheckBox checkBox_main_hobby1;

private CheckBox checkBox_main_hobby2;

private CheckBox checkBox_main_hobby3;

private CheckBox checkBox_main_hobby4;

private CheckBox checkBox_main_selectall;

private Button button_main_submit;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

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

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

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

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

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

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

button_main_submit.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(MainActivity.this, "您选择了:" + getResult(),

Toast.LENGTH_SHORT).show();

}

});

// 定义一个有名字的监听器类。之所以不用匿名内部类形式,是因为有多个控件都要使用这同一个监听器

OnCheckedChangeListener listener = new OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView,

boolean isChecked) {

if (!buttonView.isChecked()) {

checkBox_main_selectall.setChecked(false);

}

if (checkBox_main_hobby1.isChecked()

&& checkBox_main_hobby2.isChecked()

&& checkBox_main_hobby3.isChecked()

&& checkBox_main_hobby4.isChecked()) {

checkBox_main_selectall.setChecked(true);

}

Toast.makeText(MainActivity.this, "您选择了:" + getResult(),

Toast.LENGTH_SHORT).show();

}

};

checkBox_main_hobby1.setOnCheckedChangeListener(listener);

checkBox_main_hobby2.setOnCheckedChangeListener(listener);

checkBox_main_hobby3.setOnCheckedChangeListener(listener);

checkBox_main_hobby4.setOnCheckedChangeListener(listener);

// 给全选checkbox设置单击监听事件

checkBox_main_selectall.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

boolean flag = checkBox_main_selectall.isChecked();

checkBox_main_hobby1.setChecked(flag);

checkBox_main_hobby2.setChecked(flag);

checkBox_main_hobby3.setChecked(flag);

checkBox_main_hobby4.setChecked(flag);

}

});

}

// 获取多选项中被勾选的结果。利用isChecked()方法来判断哪个选项被勾选

private String getResult() {

StringBuilder sb = new StringBuilder();

if (checkBox_main_hobby1.isChecked()) {

sb.append(checkBox_main_hobby1.getText());

}

if (checkBox_main_hobby2.isChecked()) {

sb.append(checkBox_main_hobby2.getText());

}

if (checkBox_main_hobby3.isChecked()) {

sb.append(checkBox_main_hobby3.getText());

}

if (checkBox_main_hobby4.isChecked()) {

sb.append(checkBox_main_hobby4.getText());

}

return sb.toString();

}

@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;

}

}

    
六、Spinner  (下拉式列表)(一)、 类结构介绍:java.lang.Object   ↳ android.view.View    ↳ android.view.ViewGroup      ↳ android.widget.AdapterView        ↳ android.widget.AbsSpinner          ↳ android.widget.Spinner
(二)、核心代码:



  A.、UI的代码:

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical">

    

        android:id="@+id/spinner_main_edu"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />

    

        android:id="@+id/button_main_submit"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textSize="@dimen/button_main_submit_fontsize"

        android:text="提交" />

    

        android:id="@+id/text_main_info"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="" />



    B、java代码:

public class MainActivity extends Activity {

private Spinner spinner_main_edu;

private Button button_main_submit;

private TextView text_main_info;

private ArrayAdapter adapter = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

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

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

// 设置数据源

String[] strArr = new String[] { "初中", "高中", "中专", "大专", "大本", "研究生" };

spinner_main_edu = (Spinner) findViewById(R.id.spinner_main_edu);

// 构建适配器。Spinner控件常用ArrayAdapter适配器,只显示文本。

// ArrayAdapter是数组适配器。第一个参数是上下文对象或者说是环境对象,第二个参数是显示数据的布局id,

// 布局id可以自定义布局,也可以使用系统自带的布局。如果使用系统的布局,则使用android.R.layout.的形式来调用。

// 第三个参数是需要加载的数据源数组。至于是哪种类型的数组,取决于ArrayAdapter的泛型类型。

adapter = new ArrayAdapter(MainActivity.this,

android.R.layout.simple_list_item_single_choice, strArr);

// 给控件设置适配器

spinner_main_edu.setAdapter(adapter);

spinner_main_edu

.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override

public void onItemSelected(AdapterView parent,

View view, int position, long id) {

// 方法1:利用AdapterView的getItemAtPosition(position)获取item的内容

String data = parent.getItemAtPosition(position)

.toString();

// 方法2:利用AdapterView的getSelectedItem()获取item的内容

String data2 = parent.getSelectedItem().toString();

// String data3 = spinner_main_edu.getItemAtPosition(

// position).toString();

// String data4 = spinner_main_edu.getSelectedItem()

// .toString();

// 方法3:利用adapter的getItem()获取item的内容

String data3 = adapter.getItem(position);

text_main_info

.setText(data + ":" + data2 + ":" + data3);

}

@Override

public void onNothingSelected(AdapterView parent) {

}

});

// 以下代码看似正确,实际上是错误的。java.lang.RuntimeException: setOnItemClickListener

// cannot be used with a spinner.

// spinner_main_edu

// .setOnItemClickListener(new AdapterView.OnItemClickListener() {

//

// @Override

// public void onItemClick(AdapterView parent, View view,

// int position, long id) {

// String data2 = parent.getSelectedItem().toString();

// text_main_info.setText(data2 + ":" + data2);

// }

// });

button_main_submit.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String data = spinner_main_edu.getSelectedItem().toString();

text_main_info.setText(getResources().getString(

R.string.text_main_info_str)

+ data);

}

});

// XmlResourceParser pullParser = getResources().getXml(

// R.xml.citys_weather);

}

@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;

}

}

你可能感兴趣的:(android,java,开发语言)