android中RadioGroup、RadioButton、Spinner、EditText用法详解(含示例截图和源代码)

        为了保护版权、尊重原创,转载请注明出处:http://blog.csdn.net/u013149325/article/details/43237757,谢谢!

       今天在项目中用到了android中常用的RadioGroup、RadioButton、Spinner、EditText等控件,在此介绍一下它们的用法,希望对需要的朋友有帮助。

      一、RadioGroup和RadioButton的使用

            RadioButton就是我们常见的单选按钮,一个RadioGroup可以包含多个单选按钮,但是每次只能选择其中的一个值。

      我们先看一下布局文件:

<span style="font-size:18px;"><RadioGroup
            android:id="@+id/quickResponseRG"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/ignore_quick_response"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/yesRadioButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/ignore_yes"
                android:textSize="22sp" />

            <RadioButton
                android:id="@+id/noRadioButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/ignore_no"
                android:textSize="22sp" />
        </RadioGroup></span>
       再来看一下RadioGroup的监听器接口:RadioGroup.OnCheckedChangeListener

 public interface OnCheckedChangeListener {
        /**
         * <p>Called when the checked radio button has changed. When the
         * selection is cleared, checkedId is -1.</p>
         *
         * @param group the group in which the checked radio button has changed
         * @param checkedId the unique identifier of the newly checked radio button
         */
        public void onCheckedChanged(RadioGroup group, int checkedId);
    }
        我们需要实现RadioGroup.OnCheckedChangeListener接口,重写onCheckedChanged(RadioGroup group, int checkedId)函数,参数group指定添加监听器的RadioGroup,参数checkedId指定选中的RadioButton的ID。

      二、Spinner的使用

          Spinner是下拉列表选择框,可以绑定数据源,数据源可以在程序中定义,也可以在values/strings中定义字符串数组,还可以在数据库中查询获取。

        布局很简单,详见后面的例子。

        绑定数据源:

String[] intervalTime = getResources().getStringArray(R.array.intervalTime);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, intervalTime);
		bstIntervalSpinner.setAdapter(adapter);
       设置监听器接口:AdapterView.OnItemSelectedListener

public interface OnItemSelectedListener {
        /**
         * <p>Callback method to be invoked when an item in this view has been
         * selected. This callback is invoked only when the newly selected
         * position is different from the previously selected position or if
         * there was no selected item.</p>
         *
         * Impelmenters can call getItemAtPosition(position) if they need to access the
         * data associated with the selected item.
         *
         * @param parent The AdapterView where the selection happened
         * @param view The view within the AdapterView that was clicked
         * @param position The position of the view in the adapter
         * @param id The row id of the item that is selected
         */
        void onItemSelected(AdapterView<?> parent, View view, int position, long id);

        /**
         * Callback method to be invoked when the selection disappears from this
         * view. The selection can disappear for instance when touch is activated
         * or when the adapter becomes empty.
         *
         * @param parent The AdapterView that now contains no selected item.
         */
        void onNothingSelected(AdapterView<?> parent);
    }
     我们需要实现AdapterView.OnItemSelectedListener接口,重写onItemSelected(AdapterView<?> parent, View view, int position, long id)方法。

    三、EditText的使用

       EditText很常见,用来输入文本,给它添加监听器可以实时监测已经输入的文本内容,包括查看是否有错误、输入是否符合规范、长度是否超出了范围。

    添加监听器:

powerEditText.addTextChangedListener(editTextListener);
    监听器实现TextWatcher接口:

public interface TextWatcher extends NoCopySpan {
    /**
     * This method is called to notify you that, within <code>s</code>,
     * the <code>count</code> characters beginning at <code>start</code>
     * are about to be replaced by new text with length <code>after</code>.
     * It is an error to attempt to make changes to <code>s</code> from
     * this callback.
     */
    public void beforeTextChanged(CharSequence s, int start,
                                  int count, int after);
    /**
     * This method is called to notify you that, within <code>s</code>,
     * the <code>count</code> characters beginning at <code>start</code>
     * have just replaced old text that had length <code>before</code>.
     * It is an error to attempt to make changes to <code>s</code> from
     * this callback.
     */
    public void onTextChanged(CharSequence s, int start, int before, int count);

    /**
     * This method is called to notify you that, somewhere within
     * <code>s</code>, the text has been changed.
     * It is legitimate to make further changes to <code>s</code> from
     * this callback, but be careful not to get yourself into an infinite
     * loop, because any changes you make will cause this method to be
     * called again recursively.
     * (You are not told where the change took place because other
     * afterTextChanged() methods may already have made other changes
     * and invalidated the offsets.  But if you need to know here,
     * you can use {@link Spannable#setSpan} in {@link #onTextChanged}
     * to mark your place and then look up from here where the span
     * ended up.
     */
    public void afterTextChanged(Editable s);
}
     我们可以根据需要重写beforeTextChanged(CharSequence s, int start, int count, int after)、onTextChanged(CharSequence s, int start, int before, int count)、afterTextChanged(Editable s)这三个方法。

   四、示例和源代码

     先上效果图:


       在上图中,有3个RadioGroup、2个Spinner和1个EditText,由于程序中设置了监听器,所以上面选项设置的值都会在分割线下面实时显示出结果。

源代码:

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

        <TextView
            android:id="@+id/ignoreTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/ignore_quick_response"
            android:textSize="22sp" />

        <RadioGroup
            android:id="@+id/quickResponseRG"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/ignore_quick_response"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/yesRadioButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/ignore_yes"
                android:textSize="22sp" />

            <RadioButton
                android:id="@+id/noRadioButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/ignore_no"
                android:textSize="22sp" />
        </RadioGroup>
    </LinearLayout>

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

        <TextView
            android:id="@+id/BSTintervalTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/BST_interval"
            android:textSize="22sp" />

        <Spinner
            android:id="@+id/BSTintervalSpinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/trans_retry"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/trans_retry_interval"
            android:textSize="22sp" />

        <Spinner
            android:id="@+id/transRetrySpinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/powerTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/power"
            android:textSize="22sp" />

        <EditText
            android:id="@+id/powerEditText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:text="@string/default_power"
            android:textSize="22sp" />

        <TextView
            android:id="@+id/powerRangeTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/power_range"
            android:textSize="22sp" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/channelTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/channel_num"
            android:textSize="22sp" />

        <RadioGroup
            android:id="@+id/channelNumRG"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/ignore_quick_response"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/channelRadioButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/zero"
                android:textSize="22sp" />

            <RadioButton
                android:id="@+id/channelRadioButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/one"
                android:textSize="22sp" />
        </RadioGroup>
    </LinearLayout>

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

        <TextView
            android:id="@+id/crcTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/crc_check"
            android:textSize="22sp" />

        <RadioGroup
            android:id="@+id/crcCheckRG"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/ignore_quick_response"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/crcRadioButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/zero"
                android:textSize="22sp" />

            <RadioButton
                android:id="@+id/crcRadioButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/one"
                android:textSize="22sp" />
        </RadioGroup>
    </LinearLayout>

    <View 
        android:id="@+id/line"
        android:layout_width="match_parent"
        android:layout_height="2dip"
        android:background="#FF909090"/>
    
    <TextView 
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/setResult"
        android:textSize="22sp"/>
    
    <TextView 
        android:id="@+id/result1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="22sp"/>
    
    <TextView 
        android:id="@+id/result2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="22sp"/>
    
    <TextView 
        android:id="@+id/result3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="22sp"/>
    
    <TextView 
        android:id="@+id/result4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="22sp"/>
    
    <TextView 
        android:id="@+id/result5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="22sp"/>
    
    <TextView 
        android:id="@+id/result6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="22sp"/>
    
</LinearLayout>
    Java类文件:

package readAndWriteOBU;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import com.hustxks.etcapp.R;

public class InitActivity extends Activity{
	//忽略快速响应帧单选框
	private RadioGroup quickResponseRadioGroup;
	private RadioButton yesRadioButton;
	private RadioButton noRadioButton;
	
	//BST间隔下拉列表
	private Spinner bstIntervalSpinner;
    //交易重试间隔下拉列表
	private Spinner transIntervalSpinner;
	//功率级数编辑框
	private EditText powerEditText;
	
	//信道号单选框
	private RadioGroup channelNumRadioGroup;
	private RadioButton channelRadioButton1;
	private RadioButton channelRadioButton2;
	
	//CRC校验单选框
	private RadioGroup crcCkeckRadioGroup;
	private RadioButton crcRadioButton1;
	private RadioButton crcRadioButton2;
	
	//是否忽略快速响应帧标志:00:忽略快速响应 01:完全透传
	public static String ignoreQuickResFlag = "0";
	//BST间隔,单位ms,范围1~10ms
	public static String bstInterval = "10";
	//交易重试间隔,单位ms,范围1~10ms
	public static String transRetryInterval = "10";
	//功率级数,范围0~31
	public static String power = "5";
	//信道号,取值0,1
	public static String channelID = "0";
	//CRC校验标志位,取值0,1
	public static String crcCheckFlag = "0";
	
	private String[] intervalTime;
	
	//显示设置结果文本框
	TextView resultTextView1;
	TextView resultTextView2;
	TextView resultTextView3;
	TextView resultTextView4;
	TextView resultTextView5;
	TextView resultTextView6;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.init_activity);
		initView();
		RadioGroupListener quickRadioGroupListener = new RadioGroupListener(quickResponseRadioGroup);
		RadioGroupListener channelRadioGroupListener = new RadioGroupListener(channelNumRadioGroup);
		RadioGroupListener crcRadioGroupListener = new RadioGroupListener(crcCkeckRadioGroup);
		EditTextListener editTextListener = new EditTextListener();
		
		quickResponseRadioGroup.setOnCheckedChangeListener(quickRadioGroupListener);
		channelNumRadioGroup.setOnCheckedChangeListener(channelRadioGroupListener);
		crcCkeckRadioGroup.setOnCheckedChangeListener(crcRadioGroupListener);
		powerEditText.addTextChangedListener(editTextListener);
		
		intervalTime = getResources().getStringArray(R.array.intervalTime);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, intervalTime);
		bstIntervalSpinner.setAdapter(adapter);
		transIntervalSpinner.setAdapter(adapter);
		SpinnerListener bstSpinnerListener = new SpinnerListener(bstIntervalSpinner);
		bstIntervalSpinner.setOnItemSelectedListener(bstSpinnerListener);
		SpinnerListener retrySpinnerListener = new SpinnerListener(transIntervalSpinner);
		transIntervalSpinner.setOnItemSelectedListener(retrySpinnerListener);
	}
	
	private void initView() {
		quickResponseRadioGroup = (RadioGroup)findViewById(R.id.quickResponseRG);
		yesRadioButton = (RadioButton)findViewById(R.id.yesRadioButton);
		noRadioButton = (RadioButton)findViewById(R.id.noRadioButton);
		
		bstIntervalSpinner = (Spinner)findViewById(R.id.BSTintervalSpinner);
		transIntervalSpinner = (Spinner)findViewById(R.id.transRetrySpinner);
		powerEditText = (EditText)findViewById(R.id.powerEditText);
		
		channelNumRadioGroup = (RadioGroup)findViewById(R.id.channelNumRG);
		channelRadioButton1 = (RadioButton)findViewById(R.id.channelRadioButton1);
		channelRadioButton2 = (RadioButton)findViewById(R.id.channelRadioButton2);
		
		crcCkeckRadioGroup = (RadioGroup)findViewById(R.id.crcCheckRG);
		crcRadioButton1 = (RadioButton)findViewById(R.id.crcRadioButton1);
		crcRadioButton2 = (RadioButton)findViewById(R.id.crcRadioButton2);
		
		resultTextView1 = (TextView)findViewById(R.id.result1);
		resultTextView2 = (TextView)findViewById(R.id.result2);
		resultTextView3 = (TextView)findViewById(R.id.result3);
		resultTextView4 = (TextView)findViewById(R.id.result4);
		resultTextView5 = (TextView)findViewById(R.id.result5);
		resultTextView6 = (TextView)findViewById(R.id.result6);
	}
	
	//监听RadioGroup的类
	class RadioGroupListener implements RadioGroup.OnCheckedChangeListener {
		private RadioGroup myRadioGroup;
		public RadioGroupListener(RadioGroup radioGroup) {
			// TODO Auto-generated constructor stub
			this.myRadioGroup = radioGroup;
		}
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub
			switch (myRadioGroup.getId()) {
			case R.id.quickResponseRG:
				if (checkedId == R.id.yesRadioButton) {
					ignoreQuickResFlag = "00";
				}
				else if (checkedId == R.id.noRadioButton) {
					ignoreQuickResFlag = "01";
				}
				resultTextView1.setText(ignoreQuickResFlag);
				break;
			case R.id.channelNumRG:
				if (checkedId == R.id.channelRadioButton1) {
					channelID = channelRadioButton1.getText().toString().trim();
				}
				else if (checkedId == R.id.channelRadioButton2) {
					channelID = channelRadioButton2.getText().toString().trim();
				}
				resultTextView5.setText(channelID);;
				break;
			case R.id.crcCheckRG:
				if (checkedId == R.id.crcRadioButton1) {
					crcCheckFlag = crcRadioButton1.getText().toString().trim();
				}
				else if (checkedId == R.id.crcRadioButton2) {
					crcCheckFlag = crcRadioButton2.getText().toString().trim();
				}
				resultTextView6.setText(crcCheckFlag);
				break;
			default:
				break;
			}
		}
	}
	
	//监听EditText的类
	class EditTextListener implements TextWatcher {
		@Override
		public void beforeTextChanged(CharSequence s, int start,
                int count, int after) {
			
		}
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			
		}
		@Override
		public void afterTextChanged(Editable s) {
			power = powerEditText.getText().toString().trim();
			resultTextView4.setText(power);
		}
	}
	
	//监听Spinner的类
	class SpinnerListener implements AdapterView.OnItemSelectedListener {
		private Spinner mySpinner;
		public SpinnerListener(Spinner spinner) {
			// TODO Auto-generated constructor stub
			this.mySpinner = spinner;
		}
		@Override
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			if (mySpinner.getId() == R.id.BSTintervalSpinner) {
				bstInterval = intervalTime[position];
				resultTextView2.setText(bstInterval);
			}
			else if (mySpinner.getId() == R.id.transRetrySpinner) {
				transRetryInterval = intervalTime[position];
				resultTextView3.setText(transRetryInterval);
			}
		}
		@Override
		public void onNothingSelected(AdapterView<?> parent) {
			
		}
	}
}
     到此结束,转载请注明出处,谢谢!

       

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Android应用程序,用于演示TextView、EditText、Button、ImageButton、CheckBox、RadioButtonSpinner和ListView的使用方法,并实现了对Button、ImageButton、RadioButtonSpinner和ListView的鼠标监听事件。 activity_main.xml布局文件: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- TextView控件 --> <TextView android:id="@+id/textview" android:text="这是一个TextView控件" android:textSize="20sp" android:layout_width="match_parent" android:layout_height="wrap_content"/> <!-- EditText控件 --> <EditText android:id="@+id/edittext" android:hint="请输入内容" android:layout_width="match_parent" android:layout_height="wrap_content"/> <!-- Button控件 --> <Button android:id="@+id/button" android:text="普通按钮" android:layout_width="match_parent" android:layout_height="wrap_content"/> <!-- ImageButton控件 --> <ImageButton android:id="@+id/imagebutton" android:src="@drawable/ic_launcher" android:layout_width="match_parent" android:layout_height="wrap_content"/> <!-- CheckBox控件 --> <CheckBox android:id="@+id/checkbox" android:text="复选框" android:layout_width="match_parent" android:layout_height="wrap_content"/> <!-- RadioButton控件 --> <RadioGroup android:id="@+id/radiogroup" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioButton android:id="@+id/radiobutton1" android:text="单选按钮1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioButton android:id="@+id/radiobutton2" android:text="单选按钮2" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RadioGroup> <!-- Spinner控件 --> <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/fruit_array" android:prompt="@string/choose_fruit"/> <!-- ListView控件 --> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> ``` MainActivity.java代码文件: ```java import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ImageButton; import android.widget.ListView; import android.widget.RadioButton; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private TextView textView; private EditText editText; private Button button; private ImageButton imageButton; private CheckBox checkBox; private RadioButton radioButton1; private RadioButton radioButton2; private Spinner spinner; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取控件对象 textView = findViewById(R.id.textview); editText = findViewById(R.id.edittext); button = findViewById(R.id.button); imageButton = findViewById(R.id.imagebutton); checkBox = findViewById(R.id.checkbox); radioButton1 = findViewById(R.id.radiobutton1); radioButton2 = findViewById(R.id.radiobutton2); spinner = findViewById(R.id.spinner); listView = findViewById(R.id.listview); // 给普通按钮添加点击事件监听器 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String text = editText.getText().toString(); textView.setText(text); } }); // 给图片按钮添加点击事件监听器 imageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "图片按钮被点击了", Toast.LENGTH_SHORT).show(); } }); // 给单选按钮添加点击事件监听器 radioButton1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "单选按钮1被选了", Toast.LENGTH_SHORT).show(); } }); radioButton2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "单选按钮2被选了", Toast.LENGTH_SHORT).show(); } }); // 给下拉框添加选择事件监听器 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { String fruit = parent.getItemAtPosition(position).toString(); Toast.makeText(MainActivity.this, "你选择了" + fruit, Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); // 设置ListView的数据适配器 List<String> data = new ArrayList<>(); data.add("苹果"); data.add("香蕉"); data.add("橙子"); ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, data); listView.setAdapter(adapter); // 给ListView添加点击事件监听器 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String fruit = parent.getItemAtPosition(position).toString(); Toast.makeText(MainActivity.this, "你点击了" + fruit, Toast.LENGTH_SHORT).show(); } }); } } ``` 其,需要在res/values目录下创建一个arrays.xml文件,定义下拉框的数据项: ```xml <resources> <string name="app_name">MyApp</string> <string name="choose_fruit">请选择一种水果</string> <string-array name="fruit_array"> <item>苹果</item> <item>香蕉</item> <item>橙子</item> <item>草莓</item> <item>葡萄</item> </string-array> </resources> ``` 运行该应用程序,即可看到界面上显示了各种控件,并且可以通过监听事件来实现相应的交互效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值