Android入门-SayHello及其多种实现方法

目标效果


实现方法

一、使用内部类

1、新建项目SayHello

2、在activity_main.xml中设置页面布局

<span style="font-size:18px;"><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"
    android:background="@drawable/bg"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tvNamePrompt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="130dp"
        android:textSize="25sp"
        android:text="@string/tvNamePrompt">
        
    </TextView>

    <EditText
        android:id="@+id/etName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvNamePrompt"
        android:layout_below="@+id/tvNamePrompt"
        android:layout_marginTop="25dp"
        android:ems="8" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btnClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/etName"
        android:layout_below="@+id/etName"
        android:layout_marginRight="46dp"
        android:layout_marginTop="22dp"
        android:text="@string/btnClick" />
</RelativeLayout></span><span style="font-size: 24px;">
</span>

3、在MainActivity.java中新建内部类,并为点击事件设置点击事件监听者

<span style="font-size:18px;">public class MainActivity extends Activity {
	private Button btnClick;
	private EditText etName;
    //private static final String TAG="MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnClick=(Button) findViewById(R.id.btnClick);
        etName=(EditText) findViewById(R.id.etName);
        btnClick.setOnClickListener(new ClickHandler());
    }
    class ClickHandler implements OnClickListener{//新建内部类ClickHandler

		@Override
		public void onClick(View v) {
			String name=etName.getText().toString();
			//Log.d(TAG, "Hello"+name);
			Toast.makeText(MainActivity.this, "你好"+name,Toast.LENGTH_LONG).show();
			// TODO Auto-generated method stub
			
			
			
		}
    	
    }

}</span>
4、strings.xml代码
</pre><pre name="code" class="html"><span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">SayHello</string>
    <string name="action_settings">Settings</string>
    <string  name="tvNamePrompt">请输入你的姓名:</string>
    <string  name="btnClick">请点击我</string>
    <string name="etName"></string>

</resources></span>


二、使用匿名内部类

只需修改MainActivity.java即可

<span style="font-size:18px;">public class MainActivity extends Activity {
	private Button btnClick;
	private EditText etName;
    private static final String TAG="MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnClick=(Button) findViewById(R.id.btnClick);
        etName=(EditText) findViewById(R.id.etName);
        <strong>btnClick.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				String name=etName.getText().toString();
    			Log.d(TAG, "Hello"+name);
    			Toast.makeText(MainActivity.this, "你好"+name,Toast.LENGTH_LONG).show();
				// TODO Auto-generated method stub
				
			}
		
       
        });</strong>
    }
    
    	
}</span>


三、属性文件:属性触发

1、activity_main.xml代码

<span style="font-size:18px;"><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"
    android:background="@drawable/bg"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tvNamePrompt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="130dp"
        android:textSize="25sp"
        android:text="@string/tvNamePrompt">
        
    </TextView>

    <EditText
        android:id="@+id/etName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvNamePrompt"
        android:layout_below="@+id/tvNamePrompt"
        android:layout_marginTop="25dp"
        android:ems="8" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btnClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/etName"
        android:layout_below="@+id/etName"
        android:layout_marginRight="46dp"
        android:layout_marginTop="22dp"
        android:text="@string/btnClick"
        <strong>android:onClick="SayHello"</strong> />

</RelativeLayout></span>

2、MainActivity.java(此时不需要再定义Button)

<span style="font-size:18px;">public class MainActivity extends Activity {
	private EditText etName;
    private static final String TAG="MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etName=(EditText) findViewById(R.id.etName);
        
    }
    public void SayHello(View v){
    	String name=etName.getText().toString();
		Log.d(TAG, "Hello"+name);
		Toast.makeText(MainActivity.this, "你好"+name,Toast.LENGTH_LONG).show();
    }
			


}</span>

















已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页