Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

【Android】控件和基本事件响应的三种方式

$
0
0

第一种方式

package com.app;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainActivity.this,SecondActivity.class);
				startActivity(intent);
			}
		});
	}
}

第二种方式

package com.app;

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;


public class SecondActivity extends Activity implements OnClickListener{
	private Button button1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcome);
		button1 = (Button) findViewById(R.id.button1);
		button1.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button1:
			Intent intent = new Intent();
			intent.setClass(SecondActivity.this, ThirdActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}
}

第三种方式

package com.app;

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;

public class ThirdActivity extends Activity{
	private Button_OnClick mbutton = new Button_OnClick();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_third);
		Button button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(mbutton);
	}
	class Button_OnClick implements OnClickListener{
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.button1:
				Intent intent = new Intent();
				intent.setClass(ThirdActivity.this, MainActivity.class);
				startActivity(intent);
				break;

			default:
				break;
			}
		}
	}
}


作者:oYunTaoLianWu 发表于2013-9-18 2:31:14 原文链接
阅读:79 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>