您的位置:首页 > 移动开发 > Android开发

Click++项目

2016-02-01 23:52 621 查看
实现点击click按钮,数字加1。

//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="com.wl.click.MainActivity" >

<TextView
android:id="@+id/textView"
android:layout_centerHorizontal="true"
android:textColor="#FF0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="80dp"
android:text="0" />

<Button
android:id="@+id/button"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click"
/>

</RelativeLayout>


//MainActivity.java
package com.wl.click;

import android.app.Activity;
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.TextView;

public class MainActivity extends Activity {

private TextView textView;
private Button button;
public int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView = (TextView)findViewById(R.id.textView);
button = (Button)findViewById(R.id.button);

ButtonListener buttonListener = new ButtonListener(); //生成监听器对象
button.setOnClickListener(buttonListener);//为button控件绑定监听器对象buttonListener
}

class ButtonListener implements OnClickListener{

@Override
public void onClick(View v) {
count++;
textView.setText(count + "");

}

}
@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);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android开发