Android——按钮操作

来源:互联网 发布:阿里云网站模板上传 编辑:程序博客网 时间:2024/05/15 21:52

     本文主要讲解Android的按钮操作,希望有所帮助。

如下包括

1、点击按钮后,toast提示

2、点击按钮后,拨打电话

3、点击按钮后,发送信息

4、点击按钮后,跳转其他activity


具体代码如下:

MainActivity.java

package com.my.hello;import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.File;import java.io.IOException;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLDecoder;import java.net.URLEncoder;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.app.Activity;import android.content.Intent;import android.util.Log;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import android.widget.VideoView;public class MainActivity extends Activity {        //以下为定义私有变量private TextView tv = null ;private EditText ev = null ;private Button bt1 = null ;private Button bt2 = null ;private Button bt3 = null ;private Button bt4 = null ;private final static int REQUESTCODE = 1 ;private static final String CONTENT = "save" ;private VideoView videoView;  private static final String VIDEO_PATH = Environment          .getExternalStorageDirectory()          + File.separator          + "mymovie"          + File.separator + "shenghuaweiji.mp4";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);                //此处为获取布局文件中的控件ev = (EditText) findViewById(R.id.et1);bt1 = (Button) findViewById(R.id.bt1) ;bt2 = (Button) findViewById(R.id.bt2) ;bt3 = (Button) findViewById(R.id.bt3) ;bt4 = (Button) findViewById(R.id.bt4) ;if(savedInstanceState!=null && savedInstanceState.containsKey(CONTENT)){ev.setText(savedInstanceState.getString(CONTENT)) ;Log.i("tttt","test 123 "+ savedInstanceState.getString(CONTENT) ) ;}//对按钮进行点击操作bt1.setOnClickListener(buttonOnClick) ;bt2.setOnClickListener(buttonOnClick) ;bt3.setOnClickListener(buttonOnClick) ;bt4.setOnClickListener(buttonOnClick) ;Log.i("tes","000") ;//post method send message and receive message,在本文中尚未进行如何处理,可以参考本博客的其他文章String strUrl = "http://172.27.251.173:8081/AndroidServlet/ServletText" ;URL url = null ;try {Log.i("tes","001") ; url = new URL(strUrl) ;HttpURLConnection urlConn = (HttpURLConnection) url.openConnection() ;urlConn.setDoInput(true);urlConn.setDoOutput(true);urlConn.setRequestMethod("Post");urlConn.setUseCaches(true);urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");urlConn.setRequestProperty("Charset", "utf-8");urlConn.connect(); DataOutputStream dop = new DataOutputStream(urlConn.getOutputStream()) ;dop.writeBytes("param=" + URLEncoder.encode("A: 下周去钓鱼","utf-8")); //send paramdop.flush();dop.close();//send messageBufferedReader br = new BufferedReader(new InputStreamReader(urlConn.getInputStream())) ;String result = "" ;String readLine = null ;while((readLine = br.readLine())!= null){result += readLine ;}br.close();urlConn.disconnect();//show received messageev.setText(URLDecoder.decode(result,"utf-8"));} catch (MalformedURLException e){e.printStackTrace(); }catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}        //定义监听器,通过传递不同的参数来实现不同的功能。private OnClickListener buttonOnClick = new OnClickListener() {@Overridepublic void onClick(View v) {Button bt = (Button) v ;switch (bt.getId()) {                        //本监听器实现:点击按钮后,自动设置edittext的内容,然后给出相关的toast提示                        case R.id.bt1:Log.i("tttt","button ����") ;ev.setText("button above input new text") ;ev.setSelection(ev.getText().length()) ;Toast.makeText(MainActivity.this, "��ť1 input text is changed",Toast.LENGTH_SHORT).show() ;Log.i("tes","00XX") ; break;        //本监听器实现:点击按钮后,通过intent来打开拨号程序,然后设置拨号号码进行拨号。case R.id.bt2://ev.setText("button blow input new text") ;//Toast.makeText(MainActivity.this, "��ť2 input text is changed",Toast.LENGTH_SHORT).show() ;Intent intent_call = new Intent() ;intent_call.setAction(Intent.ACTION_CALL) ;intent_call.setData(Uri.parse("tel:10086")) ;startActivity(intent_call) ;break;                        //本监听器实现:点击按钮后,通过intent打开发送信息的程序,然后设置信息接收者、信息内容。                        case R.id.bt3:Toast.makeText(MainActivity.this, "��ť1 input text is changed",Toast.LENGTH_SHORT).show() ;Intent intent_sms = new Intent() ;intent_sms.setAction(Intent.ACTION_SENDTO) ;intent_sms.setData(Uri.parse("smsto:15011552917")) ;intent_sms.putExtra("sms_body", "hello") ;startActivity(intent_sms) ;break ;                        //本监听器实现:点击按钮后,通过intent来跳转到其他页面,且附带相关的参数信息              case R.id.bt4:Intent intent_activity = new Intent() ;intent_activity.setClass(MainActivity.this, Show.class) ;intent_activity.putExtra("str", "intend_demo") ; //�����ִ�//startActivity(intent_activity) ;startActivityForResult(intent_activity, REQUESTCODE) ;}}};@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);if(requestCode == REQUESTCODE){if(resultCode == Show.RESULT_CODE){Bundle bundle = data.getExtras() ;String str = bundle.getString("back") ;Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show() ;}}}        //此处为创建menu菜单@Overridepublic 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;}@Overrideprotected void onSaveInstanceState(Bundle outState) {// TODO Auto-generated method stubsuper.onSaveInstanceState(outState);String st = ev.getText().toString() ;outState.putString(CONTENT, st) ;Log.i("tttt","test save") ;}}

show.java

//该页面为点击按钮后跳转的页面,且可以接收传送过来的参数

package com.my.hello;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;/** * @author HST00074 * */public class Show extends Activity {private EditText et = null ;private Button bt = null ;public final static int RESULT_CODE = 1 ;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_show) ;Intent intent = getIntent() ;Bundle bd = intent.getExtras() ;String sr = bd.getString("str") ;et = (EditText) findViewById(R.id.et) ;et.setText(sr) ;bt = (Button) findViewById(R.id.bt) ;bt.setOnClickListener(buttonOnClick) ;}/** * @param args */private OnClickListener buttonOnClick = new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent() ;intent.putExtra("back", "back data") ;setResult(RESULT_CODE, intent) ;finish() ;}};}

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" >    <TextView        android:id="@+id/tv1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <EditText         android:id="@+id/et1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:hint="请输入内容"        android:layout_below="@+id/tv1"        />    <Button         android:id="@+id/bt1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="请点击1"        android:layout_below="@+id/et1"                />    <Button         android:id="@+id/bt2"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="请点击2"        android:layout_below="@+id/bt1"                />        <Button         android:id="@+id/bt3"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="请点击3"        android:layout_below="@+id/bt2"                />        <Button         android:id="@+id/bt4"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="请点击4"        android:layout_below="@+id/bt3"                />    </RelativeLayout>

activity_show.xml

//show布局文件

<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/tv1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/show"                 />    <EditText        android:id="@+id/et"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/show"         android:layout_below="@+id/tv1"                />    <Button         android:id="@+id/bt"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="返回"         android:layout_below="@+id/et"                />        </RelativeLayout>

AndroidMainfest.xml

//Android配置文件

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.my.hello"    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" >                <uses-library android:name = "android.test.runner"/>        <activity            android:name="com.my.hello.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.my.hello.Show"            android:label="@string/app_name"             android:theme="@android:style/Theme.Dialog"            >        </activity>            </application>    <uses-permission         android:name="android.permission.CALL_PHONE"                />        <instrumentation         android:name="com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"         android:targetPackage="com.my.hello" /></manifest>



0 0
原创粉丝点击