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
- Android——按钮操作
- iPhone开发——按钮操作
- 黑马程序员——按钮操作
- Android:DropPopMenu — 显示位置跟随操作按钮显示的带箭头的弹出菜单
- android——ToggleButton按钮
- Android中Radio单选按钮操作
- Android——按钮的事件监听
- android笔记——点击按钮放大
- Android——按钮类控件
- 蜗牛—Android基础之按钮监听器
- Android—常用控件(二)按钮
- webdriver与浏览器_操作页面元素—单选按钮/多选按钮
- mfc操作浏览器(1)——执行超链接按钮
- IOS开发基础—按钮(button)基本操作
- 按钮操作
- Android入门(5)——多状态按钮ToggleButton、多选按钮CheckBox、RadioGroup与RadioButtont按钮
- Android基础入门教程——2.3.3 Button(按钮)与ImageButton(图像按钮)
- Android中使用4种方式实现按钮点击操作
- Mysql中的排序规则utf8_unicode_ci、utf8_general_ci的区别总结
- 【菜鸟学Java】8:List实体集合中保证产品名称不重复
- 迈向成功那一步真难
- Android---3种方式限制EditView输入字数
- js判断input是否为空
- Android——按钮操作
- Sqoop将SQLServer数据导入HBase
- Android EditText 明文变为密文
- OJ 系列之删除字符串中出现次数最少的字符
- Android设计模式之一个例子让你彻底明白工厂模式(Factory Pattern)
- 线程池的实现(c语言)
- 解决linux下网页页面解析问题
- 我看过的
- MSSQL 各种连接-左外连接、右外连接、内连接、全连接