android学习日记(Android当中的常见控件)
2013-09-17 14:35
429 查看
Android当中的常见控件
TextView 在activity中显示 文本信息
EditText 可编辑文本框
Button
Menu
功能: 计算两个数乘积 和 menu中点退出键退出应用程序
MainActivity .java
ResultActivity.java
activity_main.xml
result.xml
string.xml
AndroidManifest.xml
TextView 在activity中显示 文本信息
EditText 可编辑文本框
Button
Menu
功能: 计算两个数乘积 和 menu中点退出键退出应用程序
MainActivity .java
package com.example.activity04; import android.R.string; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences.Editor; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; 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; /*声明4个控件 * 控件设置显示值 * 创建一个监听器 监听按钮按下的动作 * 将监听器对象绑定在安扭上 */ public class MainActivity extends Activity { private EditText factorOne; private EditText factorTwo; private TextView symbol; private Button getresult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); factorOne = (EditText)findViewById(R.id.factorOne); factorTwo = (EditText)findViewById(R.id.factorTwo); symbol = (TextView)findViewById(R.id.symbol); getresult = (Button)findViewById(R.id.getresult); //设置显示的值 //symbol.setText("乘以"); //getresult.setText("计算"); symbol.setText(R.string.symbol); getresult.setText(R.string.getresult); class getresultListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub //取两个EditText的值 String factorOnestr = factorOne.getText().toString(); String factorTwostr = factorTwo.getText().toString(); //将两个值放到Intent中 Intent intent =new Intent(); intent.putExtra("one",factorOnestr); intent.putExtra("two",factorTwostr); intent.setClass(MainActivity.this, ResultActivity.class); //启动RestltActivity中 MainActivity.this.startActivity(intent); } } //将监听器绑定到按钮 getresult.setOnClickListener(new getresultListener()); getresult.setOnClickListener(new getresultListener()); } @Override //当客户点击菜单中的某一个选项运行该方法 public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId()==1){ finish(); } // TODO Auto-generated method stub return super.onOptionsItemSelected(item); } @Override //当客户点击Menu时运行 public boolean onCreateOptionsMenu(Menu menu) { menu.add(0,1,1,R.string.exit); menu.add(0,2,2,R.string.about); // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
ResultActivity.java
package com.example.activity04; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; //接受intent的值 //计算两个数乘积 //显示 public class ResultActivity extends Activity{ private TextView reultView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.result); reultView=(TextView)findViewById(R.id.result); //计算两个数乘积 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; reultView.setText(result+""); } }
activity_main.xml
<RelativeLayout 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: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=".MainActivity" > <EditText android:id="@+id/factorOne" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@+id/symbol" android:layout_alignLeft="@+id/symbol" android:layout_marginBottom="31dp" android:ems="10" /> <EditText android:id="@+id/factorTwo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@+id/getresult" android:layout_centerHorizontal="true" android:layout_marginBottom="73dp" android:ems="10" > <requestFocus /> </EditText> <TextView android:id="@+id/symbol" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@+id/factorTwo" android:layout_centerHorizontal="true" /> <Button android:id="@+id/getresult" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/factorTwo" android:layout_alignParentBottom="true" android:layout_marginBottom="166dp" /> </RelativeLayout>
result.xml
<RelativeLayout 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: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=".MainActivity" > <TextView android:id="@+id/result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>
string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">activity04</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="symbol">乘以</string> <string name="getresult">计算</string> <string name="other">结果</string> <string name="exit">退出</string> <string name="about">关于</string> </resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.activity04" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" 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.example.activity04.MainActivity" 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=".ResultActivity" android:label="@string/other" /> </application> </manifest>
相关文章推荐
- android 学习3—— Android当中的常见控件
- 学习笔记001之[Android开发视频教学].01_06_Android当中的常见控件
- Android学习-常见的UI控件 AutoCompleteTextView和MultiAutoCompleteTextView
- Weli的Android学习日记 0.1控件抖动效果
- Android学习分享-常见控件05-RadioButton和CheckBox详解
- Android学习-常见的UI控件 监听按钮的点击事件
- android_基本控件学习当中出现的问题总结及工作问题思考
- Android学习分享-常见控件02-Button详解
- Android学习整理-常见控件调用方法
- android 成长日记 5.关于progress控件和Alertdialog和layout学习
- Android学习-常见的UI控件 Button和ImageButton
- android学习日记03--常用控件tabSpec/tabHost
- Android学习-常见的UI控件 RadioGroup和RadioButton
- Android学习第6课—常见控件(一)
- Android学习-常见的UI控件 TextView、EditText和ImageView
- android学习日记03--常用控件button/imagebutton
- Android学习分享-常见控件01-TextView详解
- marsAndroid学习——常见控件DatePicker、DatePickerDialog和TimePicker、TimePickerDialog
- marsAndroid学习——常见控件ExpandableListActivityh和SimpleExpandableListAdapter的简单使用
- android学习日记03--常用控件Dialog