AIDE Android App基础教程第十三课(事件监听器-Event Listener)

第十三课,本课中您将学习用Java代码设置事件监听器 运行该应用程序,它显示了一个按钮和一个文本

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:gravity="center"
	android:orientation="vertical">

	<Button
		android:text="Button"
		android:textSize="20dp"
		android:layout_height="wrap_content"
		android:layout_width="wrap_content" />

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Press the button"
		android:textSize="20dp"
		android:layout_marginTop="20dp"/>

</LinearLayout>

首先通过设置“android:id”的属性值为“@+id/mainTextView1”分配一个ID给“TextView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:gravity="center"
	android:orientation="vertical">

	<Button
		android:text="Button"
		android:textSize="20dp"
		android:layout_height="wrap_content"
		android:layout_width="wrap_content"/>

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Press the button"
		android:textSize="20dp"
		android:layout_marginTop="20dp"
		android:id="@+id/mainTextView1"/>

</LinearLayout>

下一步分配一个ID“mainButton1”给“Button

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:gravity="center"
	android:orientation="vertical">

	<Button
		android:text="Button"
		android:textSize="20dp"
		android:layout_height="wrap_content"
		android:layout_width="wrap_content"
		android:id="@+id/mainButton1"/>

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Press the button"
		android:textSize="20dp"
		android:layout_marginTop="20dp"
		android:id="@+id/mainTextView1"/>

</LinearLayout>

在“onCreate”方法里取得“TextView”对象的“findViewById”并分配给变量“TextView textView

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
        TextView textView=(TextView)findViewById(R.id.mainTextView1);
    }
}

下面再获取“Button”对象的“findViewById”并将其分配给变量“Button

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
        TextView textView=(TextView)findViewById(R.id.mainTextView1);
        Button button=(Button)findViewById(R.id.mainButton1);
    }
}

设置按钮点击事件监听器“setOnClickListener”,单击该按钮时,“setOnClickListener”方法被调用

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
        
        final TextView textView = (TextView) findViewById(R.id.mainTextView1);
        Button button = (Button) findViewById(R.id.mainButton1);
        
        button.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View p1)
				{
					// TODO: Implement this method
					
				}
			});
		
    }
}

在“onClick”方法里设置“TextView”对象的“setText()”方法的文本为“Clicked

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
        
        final TextView textView = (TextView) findViewById(R.id.mainTextView1);
        Button button = (Button) findViewById(R.id.mainButton1);
        
        button.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View p1)
				{
					// TODO: Implement this method
					textView.setText("Clicked");
				}
			});
		
    }
}

在“Button”对象上用“setOnLongClickListener()”设置“OnLongClickListener

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
        
        final TextView textView = (TextView) findViewById(R.id.mainTextView1);
        Button button = (Button) findViewById(R.id.mainButton1);
        
        button.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View p1)
				{
					// TODO: Implement this method
					textView.setText("Clicked");
				}
			});
		button.setOnLongClickListener(new OnLongClickListener(){

                @Override
                public boolean onLongClick(View p1) {
                    return false;
                }
            });
    }
}

现在在“onLongClick”方法里设置“TextView”对象“setText()”方法的文本为“Long Clicked

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
        
        final TextView textView = (TextView) findViewById(R.id.mainTextView1);
        Button button = (Button) findViewById(R.id.mainButton1);
        
        button.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View p1)
				{
					// TODO: Implement this method
					textView.setText("Clicked");
				}
			});
			
		button.setOnLongClickListener(new OnLongClickListener() {

				@Override
				public boolean onLongClick(View p1)
				{
					// TODO: Implement this method
					textView.setText("Long Clicked");
					return false;
				}
			});
    }
				}

最终设置“onLongClick”方法的返回值为“true”,告诉Android系统,该事件已经被处理

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
        
        final TextView textView = (TextView) findViewById(R.id.mainTextView1);
        Button button = (Button) findViewById(R.id.mainButton1);
        
        button.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View p1)
				{
					// TODO: Implement this method
					textView.setText("Clicked");
				}
			});
			
		button.setOnLongClickListener(new OnLongClickListener() {

				@Override
				public boolean onLongClick(View p1)
				{
					// TODO: Implement this method
					textView.setText("Long Clicked");
					return true;
				}
			});
    }
				}

欢迎加入AIDE教程网官方交流群:758110864

 版权声明:本文为AIDE教程网原创文章,转载请附上原文出处链接和本声明
本文链接: https://www.aidemx.cn/1901.html

梦雪

专注于AIDE教程分享

留下你的评论

*评论支持代码高亮<pre class="prettyprint linenums">代码</pre>

相关推荐