Android基础04-基本控件及表单三大控件

一、控件回顾:

(一)、需要掌握的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的可选项:

android:inputType="textPersonName"

android:inputType="textPassword"

android:inputType="numberPassword"

android:inputType="textEmailAddress"

android:inputType="phone"

android:inputType="textPostalAddress"

android:inputType="time"

android:inputType="date"

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或false

3、 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()

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

        


四、基本控件:—— 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 MainActivityextendsActivity {

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(newOnClickListener() {

        @Override

        public void onClick(View v) {

        // 获取勾选项的id

        intid = 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(newOnCheckedChangeListener() {

    @Override

    public void onCheckedChanged(RadioGroup group,intcheckedId) {

        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 MainActivityextendsActivity {

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(newOnClickListener() {

    @Override

    public void onClick(View v) {

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

    Toast.LENGTH_SHORT).show();

}

});

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

OnCheckedChangeListener listener =newOnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView,

booleanisChecked) {

    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(newOnClickListener() {

@Override

public void onClick(View v) {

    booleanflag = 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() {

String Builder sb =newStringBuilder();

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 MainActivityextendsActivity {

    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 =newString[] { "初中", "高中", "中专", "大专", "大本", "研究生" };

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

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

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

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

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

    adapter =newArrayAdapter(MainActivity.this,

    android.R.layout.simple_list_item_single_choice, strArr);

    // 给控件设置适配器

    spinner_main_edu.setAdapter(adapter);

    spinner_main_edu

.    setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {

        @Override

        public void onItemSelected(AdapterView parent,

        View view,intposition,longid) {

        // 方法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 voidonNothingSelected(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(newView.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;

}

}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容