AIDE_Android App教程答案_第十二课(组件id)

第十二课,本课中您将学习设置组件的id与Java代码关联 运行程序会显示一个有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>

我们已经在按钮的“LinearLayout”下添加了一个“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">

	<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>
	
	<TextView
		android:text="TODO: Show the result"
		android:layout_marginTop="20dp"
		android:textSize="20dp"
		android:layout_height="wrap_content"
		android:layout_width="wrap_content" />

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

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

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

</LinearLayout>

现在添加“android:onClick”的值“onYesButtonClick”到“Yes”按钮

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

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

</LinearLayout>

我们已经添加了方法“onYesButtonClick”下面我们要改变“TextView”的文本

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

要获取“TextView”的对象,可以通过ID为“R.id.mainTextView1”的方法来实现

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) 
    {
    	TextView textView = (TextView) findViewById(R.id.mainTextView1);
    	
	}
	
	
}

现在调用“TextView”对象的方法“setText”的返回值“Correct!

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) 
    {
    	TextView textView = (TextView) findViewById(R.id.mainTextView1);
    	textView.setText("Correct!");
	}
	
	
}

添加“android:onClick”的值“onNoButtonClick”到“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"
            android:onClick="onYesButtonClick"/>

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

	</LinearLayout>

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

</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) 
    {
    	TextView textView = (TextView) findViewById(R.id.mainTextView1);
    	textView.setText("Correct!");
	}
	
	public void onNoButtonClick(View view){
        TextView textView=(TextView)findViewById(R.id.mainTextView1);
        textView.setText("Wrong!");
    }
}

最后在“onCreate”方法中获得“TextView”的“findViewById”并设置它的文本为空字符串

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);
        TextView textView=(TextView)findViewById(R.id.mainTextView1);
        textView.setText("");
    }
    
    public void onYesButtonClick(View view) 
    {
    	TextView textView = (TextView) findViewById(R.id.mainTextView1);
    	textView.setText("Correct!");
	}
	
	public void onNoButtonClick(View view){
        TextView textView=(TextView)findViewById(R.id.mainTextView1);
        textView.setText("Wrong!");
    }
}

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

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

梦雪

专注于AIDE教程分享

留下你的评论

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

相关推荐