`
童梦新苑
  • 浏览: 40196 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

android小例子 供大家初学者学习(有意见的欢迎一起讨论QQ:271218983;)

阅读更多

由于才学,这个布局实在不敢恭维,请各位谅解!!
好吧,废话不多说了,来看看这个代码吧(关于这个例子本人会在后面继续添加,不断增加)!!

 

其中使用到得技术有:intent Bundle AlertDialog SMS Activity

 

首先是程序的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="0,1,2"
    android:shrinkColumns="0,1,2"
    >
<TextView  
	android:id="@+id/hello"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    
<!--//添加两个输入框(使用TabLayout进行布局)	-->
	<TableLayout
		android:id="@+id/table"
		android:layout_width="fill_parent"
		android:layout_height="match_parent"
		android:layout_below="@id/hello"
		>
		<TableRow>
			 <EditText
		        android:id="@+id/one"
		        android:gravity="left"
		        android:layout_width="fill_parent"
		        android:layout_height="fill_parent"
		        android:background="@android:drawable/editbox_background"
		        android:layout_below="@id/hello"/>
			<TextView
				android:id="@+id/fangfa"
				android:gravity="left"
				android:layout_width="fill_parent"
				android:layout_height="fill_parent"
				android:text="X"/>
			 <EditText
		        android:id="@+id/two"
		        android:gravity="left"
		        android:layout_column="1"
		        android:layout_width="fill_parent"
		        android:layout_height="fill_parent"
		        android:background="@android:drawable/editbox_background"
		        android:layout_below="@id/one"/>
		     
	        </TableRow>
	        
	        <TableRow android:stretchColumns="">
	        	<Button 
				    android:id="@+id/cacl" 
					android:layout_width="match_parent" 
					android:layout_height="wrap_content" 
					android:layout_below="@id/table"
					android:layout_alignParentLeft="true"
					android:text="@string/caleValue" />
				 <Button 
					 android:id="@+id/smstoId" 
					 android:layout_column="1"
					 android:layout_width="match_parent" 
					 android:layout_height="match_parent" 
					 android:layout_below="@id/table"
					 android:layout_alignRight="@id/cacl"
					 android:text="@string/smstoValue"/>
	        </TableRow>
	</TableLayout>
<!--添加两个控件分别启动两个应用(短信应用  计算器Demo)-->
	 
</RelativeLayout>

 然后是另一个Activity的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TextView
 		android:id="@+id/resultTitleId"
 		android:layout_width="wrap_content"
 		android:layout_height="wrap_content"
 		android:text="@string/resultValue"
 		/>
<!-- 		再添加一个 文本控件进行Result值的显示-->
<TextView 
	android:id="@+id/resultId"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	/>
</LinearLayout>

 

然后就是资源文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">应用总结(Activity)</string>
    <string name="app_name">Activity阶段总结</string>
	<string name="smstoValue">求助短信</string>
	<string name="caleValue">计算器</string>
	
	
<!--	Result.xml配置文件中控件名称-->
	<string name="resultValue">等于:</string>
	
	<string name="menuValue1">退出</string>
	<string name="outOk">确认退出</string>
	<string name="outCancel">取消退出</string>
	<string name="menuValue2">关于</string>
</resources>

 然后是Mainifest.xml

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="zhou.activity"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ActivityDemo"
                  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=".Result" android:label="@string/app_name"></activity>
    </application>
</manifest>

 最后就是相关的程序

 

package zhou.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
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;
/**
 * ?1:如果用一个公共的监听类 来控制多个点击事件   
 * ?2:
 * @author 周周
 * 完成Activity操作  sms Intent Listener Uri
 */
public class ActivityDemo extends Activity {
    private Button smsButton = null;
    private Button caclButton = null;
    private EditText editTextOne = null;
    private EditText editTextTwo = null;
    
    /**
     * 得到相应的控件
     * 再添加相应的监听
     * 
     * 添加一个公共的监听  在监听里面进行点击事件的分类
     */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        smsButton = (Button)this.findViewById(R.id.smstoId);
        caclButton = (Button)this.findViewById(R.id.cacl);
        editTextOne = (EditText)this.findViewById(R.id.one);
        editTextTwo = (EditText)this.findViewById(R.id.two);
        
        smsButton.setOnClickListener(new ButtonListener());
        caclButton.setOnClickListener(new ButtonListener()); 
    }

    private class ButtonListener implements OnClickListener{
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.cacl:
				String oneStr = editTextOne.getText().toString();///获取两个//关联的代码输入款的值
				String TwoStr = editTextTwo.getText().toString();
				//创建传输对象     进行改建为Bundle对象
				Bundle b = new Bundle();
				b.putString("one", oneStr);
				b.putString("two", TwoStr);
				Intent intent = new Intent();
				intent.putExtras(b);///把这个存放数据的Bundle对象拿给Intent对象传递
				intent.setClass(ActivityDemo.this, Result.class);
				ActivityDemo.this.startActivity(intent);
				break;
			case R.id.smstoId:
				///代码
				Uri uri = Uri.parse("smsto:郭佳");
				Intent intent2 = new Intent(Intent.ACTION_SENDTO,uri);
				ActivityDemo.this.startActivity(intent2);
				break; 
			default:
				break;
			}
		}
    }
    
    
    
    /**
     * ///添加菜单  以及相关的处理
     */
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
    	menu.add(0,1,1,R.string.menuValue1);
    	menu.add(0,2,2,R.string.menuValue2);
		
		return super.onCreateOptionsMenu(menu);
	}



	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == 1){
			//做一个代码的增强
			new AlertDialog.Builder(ActivityDemo.this).setTitle("退出提示").setPositiveButton(R.string.outOk, new DialogInterface.OnClickListener(){
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			}).setNegativeButton(R.string.outCancel, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					System.out.println("按钮已经取消,系统不执行反应");
				}
			}).show();
		}else if(item.getItemId() == 2){
			//这个是一个关于对话框
			new AlertDialog.Builder(ActivityDemo.this).setTitle("程序作者介绍")
			.setMessage("姓名:周利军\n性别:男\nQ Q:271218983\n有意者请指教").show();
		}
		return super.onOptionsItemSelected(item);
	}

	
}

 Result.java

package zhou.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class Result extends Activity {
	private TextView resultText = null;
	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);
		
		resultText = (TextView)this.findViewById(R.id.resultId);
		///获取上一个Activity传过来的值    (程序改进 使用Bundler对数据进行封转)
		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		String oneStr = bundle.getString("one");
		String twoStr = bundle.getString("two");
		int one = Integer.parseInt(oneStr);
		int two = Integer.parseInt(twoStr);
		Integer result = one * two;
		resultText.setText(result.toString());
	}
}

 

0
3
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics