Button的使用(十):ImageButton

本文介绍了一个简单的Android应用程序示例,该程序通过点击ImageButton显示用户输入,并使用HTML标签进行文本格式化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击ImageButton时,显示用户的输入

package com.example.androidtest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.text.TextUtils;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.EditText;
import android.widget.TextView;
import android.util.Log;


public class MainActivity extends Activity  {
	
	EditText name;
	TextView result;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		name = (EditText)findViewById(R.id.name);
		result = (TextView)findViewById(R.id.result);
		
		ImageButton btn = (ImageButton)findViewById(R.id.format);
		
		btn.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				applyFormat();
			}
		});
	}
	
	private void applyFormat()
	{
		String format = getString(R.string.funky_format);
		String simpleResult = String.format(format, TextUtils.htmlEncode(name.getText().toString()));
		result.setText(Html.fromHtml(simpleResult));
	}
	
}


activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <ImageButton android:id="@+id/format"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:src="@drawable/ic_launcher"
      />
    <EditText android:id="@+id/name"
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      />
  </LinearLayout>
  <TextView android:id="@+id/result"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">AndroidTest</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="go_button">go_button</string>
	<string name="funky_format">My name is <b>%1$s</b></string>
</resources>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值