Activity(三)

常用控件

EditView、TextView、Button

设置layout,在fragment_main.xml配置控件

配置可编辑文本控件factorOne、factorTwo,显示文本控件symbol,按钮控件calculate

<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="com.example.activity01.MainActivity$PlaceholderFragment" > <EditText
android:id="@+id/factorOne"
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"
android:layout_below="@+id/sym"/>
<TextView
android:id="@+id/sym"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/factorOne" />
<Button
android:id="@+id/cal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/factorTwo"
/>
</RelativeLayout>

在values下string.xml设置symbol、calculate字符串

<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">Activity01</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="sym">乘以</string>
<string name="cal">计算</string>
</resources>

根据控件id取对象,为symbol和calculate设置显示的值

        private EditText factorOne=null;
private EditText factorTwo=null;
private TextView sym=null;
private Button cal=null;
@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);
factorOne=(EditText)findViewById(R.id.factorOne);
factorTwo=(EditText)findViewById(R.id.factorTwo);
sym=(TextView)findViewById(R.id.sym);
cal=(Button)findViewById(R.id.cal);
sym.setText(R.string.sym);
cal.setText(R.string.cal);
cal.setOnClickListener(new CalListener());//监听器绑定到按钮上
return true;
}

监听器

class CalListener implements OnClickListener{

        @Override
public void onClick(View v) {
// TODO Auto-generated method stub
String factorOneStr=factorOne.getText().toString();
String factorTwoStr=factorTwo.getText().toString();
Intent intent=new Intent();
intent.putExtra("one", factorOneStr);
intent.putExtra("two", factorTwoStr);
intent.setClass(MainActivity.this, ResultActivity.class);
MainActivity.this.startActivity(intent);
}
}

新建result.xml文件

     <TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

新建ResultActivity,获取计算结果

public class ResultActivity extends Activity{
private TextView resultView;
@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=new Intent();
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 version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activity01"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.activity01.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="com.example.activity01.ResultActivity"
android:label="@string/app_name" >
</activity>
</application> </manifest>

设置menu菜单

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
menu.add(0, 1, 1, R.string.exit);
menu.add(0, 2, 3, R.string.about);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==1){
finish();
}
return super.onOptionsItemSelected(item);
}

Copyright © 吴华锦
雅致寓于高阁渔舟唱晚,古典悠然
格调外发园林绿树萦绕,馥郁清香

  

上一篇:HttpClient和 HtmlParser实现爬虫


下一篇:【开发技术】web.xml vs struts.xml