01_06_Android当中的常见控件
常见控件(一)
TextView的使用方法
EditText的使用方法
Button的使用方法
Menu的使用方法
创建MyActivity03工程
Activity03.java
-
//Activity03.java
-
package com.lwb.myactivity03;
-
import android.os.Bundle;
-
import android.app.Activity;
-
import android.content.Intent;
-
import android.view.Menu;
-
import android.view.MenuItem;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
import android.widget.EditText;
-
import android.widget.TextView;
-
//1、Activity中要生成四个控件
-
//2、要为其中的两个空间设置显示的值
-
//3、创建一个监听器,监听按钮按下的动作
-
//4、将监听器的对象,绑在按钮对象上
-
public class Activity03 extends Activity {
-
-
//当客户点击MENU按钮是,调用该方法。Menu菜单的实现,复写一个函数,实际上是一个回调函数,点击屏幕上的Menu就能返回
-
@Override
-
public boolean onCreateOptionsMenu(Menu menu) {
-
// TODO Auto-generated method stub
-
//添加代码如下:
-
// menu.add(groupId, itemId, order, title)
-
menu.add(0, 1, 1, R.string.exit);
-
menu.add(0,2,2,R.string.about);
-
return super.onCreateOptionsMenu(menu);
-
}
-
-
//当客户点击也是一个回调函数,把点击的对象的item 传入来
-
@Override
-
public boolean onOptionsItemSelected(MenuItem item) {
-
// TODO Auto-generated method stub
-
-
if(item.getItemId()==1){//点击的是 退出按钮exit
-
finish(); //退出,即 完成
-
}
-
-
return super.onOptionsItemSelected(item);
-
}
-
-
-
-
-
private EditText factorOne;
-
private EditText factorTwo;
-
private TextView symbol;
-
private Button calculate;
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
-
//根据控件的ID来取得代表控件的对象
-
factorOne =(EditText)findViewById(R.id.factorOne);
-
factorTwo =(EditText)findViewById(R.id.factorTwo);
-
symbol =(TextView)findViewById(R.id.symbol);
-
calculate =(Button) findViewById(R.id.calculate);
-
-
//为symbol和calculate设置显示的值
-
//symbol.setText("乘以");
-
//calculate.setText("计算");
-
symbol.setText(R.string.symbol);
-
calculate.setText(R.string.calculate);
-
-
//将监听器对象绑定到按钮对象上面
-
calculate.setOnClickListener(new CalculateListener());
-
}
-
-
-
-
-
class CalculateListener implements OnClickListener{
-
-
@Override
-
public void onClick(View arg0) {
-
// TODO Auto-generated method stub
-
-
//1、取得两个EditText的值
-
String factorOneStr=factorOne.getText().toString();
-
String factorTwoStr=factorTwo.getText().toString();
-
//2、将两个值存放到Intent对象中
-
Intent intent =new Intent();
-
intent.putExtra("one", factorOneStr);
-
intent.putExtra("Two", factorTwoStr);
-
intent.setClass(Activity03.this,ResultActivity.class);
-
//3、使用这个Intent启动ResultActivity
-
Activity03.this.startActivity(intent);
-
}
-
}
-
/*@Override
-
public boolean onCreateOptionsMenu(Menu menu) {
-
// Inflate the menu; this adds items to the action bar if it is present.
-
getMenuInflater().inflate(R.menu.activity03, menu);
-
return true;
-
}*/
-
}
-
Activity03对应的布局xml文件
-
<!--main.xml -->
-
<LinearLayout xmlns:android=""
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
>
-
<EditText
-
android:id="@+id/factorOne"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
/>
-
<TextView
-
android:id="@+id/symbol"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
-
/>
-
<EditText
-
android:id="@+id/factorTwo"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
/>
-
<Button
-
android:id="@+id/calculate"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
/>
-
</LinearLayout>
-
<!--
-
<RelativeLayout xmlns:android=""
-
xmlns:tools=""
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:paddingBottom="@dimen/activity_vertical_margin"
-
android:paddingLeft="@dimen/activity_horizontal_margin"
-
android:paddingRight="@dimen/activity_horizontal_margin"
-
android:paddingTop="@dimen/activity_vertical_margin"
-
tools:context=".Activity03" >
-
<TextView
-
android:id="@+id/symbol"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_alignLeft="@+id/factorTwo"
-
android:layout_below="@+id/factorOne"
-
/>
-
-
<EditText
-
android:id="@+id/factorOne"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:layout_alignParentTop="true"
-
android:layout_centerHorizontal="true"
-
android:layout_marginTop="17dp"
-
android:ems="10" />
-
-
<EditText
-
android:id="@+id/factorTwo"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:layout_alignLeft="@+id/calculate"
-
android:layout_below="@+id/calculate"
-
android:layout_marginTop="14dp"
-
android:ems="10" />
-
-
<Button
-
android:id="@+id/calculate"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content" />
-
</RelativeLayout>
-
-->
ResultActivity.java
-
//ResultActivity.java
-
package com.lwb.myactivity03;
-
import android.app.Activity;
-
import android.content.Intent;
-
import android.os.Bundle;
-
import android.widget.TextView;
-
//1、首先接受从Activity03当中所传递的值
-
//2、计算两个值的积
-
//3、将计算的结果显示到Activity上
-
public class ResultActivity extends Activity {
-
-
private TextView resultView=null;
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
// TODO Auto-generated method stub
-
super.onCreate(savedInstanceState);
-
-
setContentView(R.layout.result);
-
resultView=(TextView)findViewById(R.id.result);
-
//得到Intent对象的值
-
Intent intent= getIntent();
-
String factorOneStr=intent.getStringExtra("one");
-
String factorTwoStr=intent.getStringExtra("Two");
-
int factorOneInt=Integer.parseInt(factorOneStr);
-
int factorTwoInt=Integer.parseInt(factorTwoStr);
-
//计算乘积
-
int result=factorOneInt*factorTwoInt;
-
resultView.setText(result+"");//整型+字符串 强制为 整型
-
}
-
}
ResultActivity它对应的布局xml文件
-
<!-- result.xml -->
-
<LinearLayout xmlns:android=""
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
>
-
<TextView
-
android:id="@+id/result"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
/>
-
</LinearLayout>
-
<!--
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android=""
-
android:orientation="vertical"
-
android:layout_with="fill_parent"
-
android:layout_height="fill_parent"
-
>
-
-
</LinearLayout>
-
-->
//string.xml
-
//string.xml
-
<?xml version="1.0" encoding="utf-8"?>
-
<resources>
-
-
<string name="app_name">MyActivity03</string>
-
<string name="action_settings">Settings</string>
-
<string name="hello_world">Hello world!</string>
-
<string name="symbol">乘以</string>
-
<string name="calculate">计算</string>
-
<string name="resultLable">结果Activity</string>
-
-
<string name="exit">退出</string>
-
<string name="about">关于</string>
-
</resources>
Manifest.xml文件
-
<!--Manifest.xml-->
-
<?xml version="1.0" encoding="utf-8"?>
-
<manifest xmlns:android=""
-
package="com.lwb.myactivity03"
-
android:versionCode="1"
-
android:versionName="1.0" >
-
-
<uses-sdk
-
android:minSdkVersion="10"
-
android:targetSdkVersion="17" />
-
-
<application
-
android:allowBackup="true"
-
android:icon="@drawable/ic_launcher"
-
android:label="@string/app_name"
-
android:theme="@style/AppTheme" >
-
<activity
-
android:name="com.lwb.myactivity03.Activity03"
-
android:label="@string/app_name" >
-
<intent-filter>
-
<action android:name="android.intent.action.MAIN" />
-
-
<category android:name="android.intent.category.LAUNCHER" />
-
</intent-filter>
-
</activity>
-
-
<activity
-
android:name="com.lwb.myactivity03.ResultActivity"
-
android:label="@string/resultLable"/>
-
<!-- 上面的android:name="com.lwb.myactivity03.ResultActivity" 是不能写成android:name=".ResultActivity"的否则会报错 -->
-
<!-- ResultActivity是第二个Activity的类名 -->
-
</application>
-
</manifest>
R.java文件
-
/* AUTO-GENERATED FILE. DO NOT MODIFY.
-
*
-
* This class was automatically generated by the
-
* aapt tool from the resource data it found. It
-
* should not be modified by hand.
-
*/
-
-
package com.lwb.myactivity03;
-
-
public final class R {
-
public static final class attr {
-
}
-
public static final class dimen {
-
/** Default screen margins, per the Android Design guidelines.
-
-
Customize dimensions originally defined in res/values/dimens.xml (such as
-
screen margins) for sw720dp devices (e.g. 10
TextView的使用方法
private TextView symbol;
symbol =(TextView)findViewById(R.id.symbol);
private TextView resultView=null;
resultView=(TextView)findViewById(R.id.result);
resultView.setText(result+"");//整型+字符串 强制为 整型
EditText的使用方法
private EditText factorOne;
private EditText factorTwo;
//根据控件的ID来取得代表控件的对象
factorOne =(EditText)findViewById(R.id.factorOne);
factorTwo =(EditText)findViewById(R.id.factorTwo);
Button的使用方法
private Button calculate;
calculate =(Button) findViewById(R.id.calculate);
calculate.setText(R.string.calculate);
Menu的使用方法
-
//当客户点击MENU按钮是,调用该方法。Menu菜单的实现,复写一个函数,实际上是一个回调函数,点击屏幕上的Menu就能返回
-
@Override
-
public boolean onCreateOptionsMenu(Menu menu) {
-
// TODO Auto-generated method stub
-
//添加代码如下:
-
// menu.add(groupId, itemId, order, title)
-
menu.add(0, 1, 1, R.string.exit);
-
menu.add(0,2,2,R.string.about);
-
return super.onCreateOptionsMenu(menu);
-
}
-
//当客户点击也是一个回调函数,把点击的对象的item 传入来
-
@Override
-
public boolean onOptionsItemSelected(MenuItem item) {
-
// TODO Auto-generated method stub
-
-
if(item.getItemId()==1){//点击的是 退出按钮exit
-
finish(); //退出,即 完成
-
}
-
return super.onOptionsItemSelected(item);
-
}
涉及到前一课的知识点(即Intent的使用,以及数据的传递):
-
//1、取得两个EditText的值
-
String factorOneStr=factorOne.getText().toString();
-
String factorTwoStr=factorTwo.getText().toString();
-
//2、将两个值存放到Intent对象中
-
Intent intent =new Intent();
-
intent.putExtra("one", factorOneStr);
-
intent.putExtra("Two", factorTwoStr);
-
intent.setClass(Activity03.this,ResultActivity.class);
-
//3、使用这个Intent启动ResultActivity
-
Activity03.this.startActivity(intent);
-
-
Intent intent= getIntent();
-
String factorOneStr=intent.getStringExtra("one");
-
String factorTwoStr=intent.getStringExtra("Two");
-
int factorOneInt=Integer.parseInt(factorOneStr);
-
int factorTwoInt=Integer.parseInt(factorTwoStr);
-
//计算乘积
-
int result=factorOneInt*factorTwoInt;
-
resultView.setText(result+"");
OK,比较简单的啦,哈哈。加油啦
阅读(754) | 评论(0) | 转发(0) |