AIDE_Android App教程答案_第十课(按钮-Button)

aide第十课

第十课,本课中您将学习到如何添加按钮到用户界面和编写点击它的代码 ,运行程序显示一个简单的文本

package com.aide.trainer.myapp;

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

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);
    }
}

在这个XML文件的“TextView”元素下面添加一个“Button”元素,也可以用UI设计器实现

<?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">

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:textSize="30dp"
		android:text="Hi World"/>

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

</LinearLayout>

请确认“Button”元素的属性“android:layout_width”和“android:layout_height”的值为“wrap_content”,这些都是强制性的,对每个视图都需要指定它的大小

<?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">

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:textSize="30dp"
		android:text="Hi World"/>

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

</LinearLayout>

设置“Button”的元素属性“android:text”为“Exit

<?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">

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:textSize="30dp"
		android:text="Hi World"/>

	<Button
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Exit"/>

</LinearLayout>

为“Button”元素添加属性“android:onClick”为“onExitButtonClick”,点击按钮时这个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">

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:textSize="30dp"
		android:text="Hi World"/>

	<Button
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Exit"
        android:onClick="onExitButtonClick"/>

</LinearLayout>

我们已经在下面的MainActivity类中添加了Java方法“onExitButtonClick

package com.aide.trainer.myapp;

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

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);
    }
    
    /** Called when the new exit button is clicked. */
    public void onExitButtonClick(View view) 
    {
        
	}
}

现在在“onExitButtonClick”中调用“finish()”方法退出应用程序

package com.aide.trainer.myapp;

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

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);
    }
    
    /** Called when the new exit button is clicked. */
    public void onExitButtonClick(View view) 
    {
        finish();
	}
}

现在在“Exit”按钮下面添加第二个按钮“Drop

<?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">

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:textSize="30dp"
		android:text="Hi World"/>

	<Button
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:onClick="onExitButtonClick"
		android:text="Exit"/>

	<Button
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Drop"/>

</LinearLayout>

添加属性“android:onClick”到“Drop”按钮,值为“onDropButtonClick

<?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">

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:textSize="30dp"
		android:text="Hi World"/>

	<Button
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:onClick="onExitButtonClick"
		android:text="Exit"/>

	<Button
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Drop"
        android:onClick="onDropButtonClick"/>

</LinearLayout>

我们已经添加了Java方法“onDropButtonClick”,运行应用程序并查看结果

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.view.*;
import android.view.animation.*;

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);
    }
    
    /** Called when the new exit button is clicked. */
    public void onExitButtonClick(View view) 
    {
        finish();
	}

    /** Called when the new drop button is clicked. */
    public void onDropButtonClick(View view) 
    {
    	// Do some fancy animation...
        TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 1000);
		animation.setDuration(1000);
		view.startAnimation(animation);
	}
}

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

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

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

梦雪

专注于AIDE教程分享

留下你的评论

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

相关推荐