AIDE_Android App教程答案_第十一课(Toast)

aide第十一课

第十一课,本课中您将学习到如何使用toast提供简单的信息反馈 运行程序会显示一个有yes-no的按钮。

<?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:text="Android is the best?"
		android:textSize="20dp"
		android:layout_margin="20dp"/>

	<LinearLayout
		android:layout_height="wrap_content"
		android:layout_width="wrap_content"
		android:orientation="horizontal">

		<Button
			android:text="Yes"
			android:layout_height="wrap_content"
			android:layout_width="wrap_content"
			android:layout_marginRight="30dp"
			android:padding="20dp"/>

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

	</LinearLayout>

</LinearLayout>

为“Yes”按钮添加“android:onClick”属性,值为“onYesButtonClick

<?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:text="Android is the best?"
		android:textSize="20dp"
		android:layout_margin="20dp"/>

	<LinearLayout
		android:layout_height="wrap_content"
		android:layout_width="wrap_content"
		android:orientation="horizontal">

		<Button
			android:text="Yes"
			android:layout_height="wrap_content"
			android:layout_width="wrap_content"
			android:layout_marginRight="30dp"
			android:padding="20dp"
            android:onClick="onYesButtonClick"/>

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

	</LinearLayout>

</LinearLayout>

我们已经添加了下面的Java方法“onYesButtonClick”来显示toast,运行程序尝试一下

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.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);
    }
    
    public void onYesButtonClick(View view) 
    {
    	// This Java code shows a toast
    	Toast.makeText(this, "Correct!", Toast.LENGTH_SHORT).show();
	}
	
	
}

将“Toast.LENGTH_SHORT”改为“Toast.LENGTH_LONG”,设置更长的提示时间

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.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);
    }
    
    public void onYesButtonClick(View view) 
    {
    	// This Java code shows a toast
    	Toast.makeText(this, "Correct!", Toast.LENGTH_LONG).show();
	}
	
	
}

现在为“No”按钮添加“android:onClick”属性值为“onNoButtonClick

<?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:text="Android is the best?"
		android:textSize="20dp"
		android:layout_margin="20dp"/>

	<LinearLayout
		android:layout_height="wrap_content"
		android:layout_width="wrap_content"
		android:orientation="horizontal">

		<Button
			android:text="Yes"
			android:layout_height="wrap_content"
			android:layout_width="wrap_content"
			android:layout_marginRight="30dp"
			android:padding="20dp"
            android:onClick="onYesButtonClick"/>

		<Button
			android:text="No"
			android:layout_height="wrap_content"
			android:layout_width="wrap_content"
			android:padding="20dp"
            android:onClick="onNoButtonClick"/>

	</LinearLayout>

</LinearLayout>

仔细观察“onYesButtonClick”方法后再添加方法“onNoButtonClick”并显示提示“Wrong!

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.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);
    }
    
    public void onYesButtonClick(View view) 
    {
    	// This Java code shows a toast
    	Toast.makeText(this, "Correct!", Toast.LENGTH_LONG).show();
	}
	
	public void onNoButtonClick(View view){
        Toast.makeText(this,"Wrong!",Toast.LENGTH_LONG).show();
    }
}

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

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

梦雪

专注于AIDE教程分享

留下你的评论

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

相关推荐