android 单选,复选按钮,以及toast

RadioTest.java

package mars.activity07;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class RadioTest extends Activity {
    /** Called when the activity is first created. */
	//对控件对象进行声明
	private RadioGroup genderGroup = null;
	private RadioButton femaleButton = null;
	private RadioButton maleButton = null;
	private CheckBox swimBox = null;
	private CheckBox runBox = null;
	private CheckBox readBox = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.radio);
        //通过控件的ID来得到代表控件的对象
        genderGroup = (RadioGroup)findViewById(R.id.genderGroup);
        femaleButton = (RadioButton)findViewById(R.id.femaleButton);
        maleButton = (RadioButton)findViewById(R.id.maleButton);
        swimBox = (CheckBox)findViewById(R.id.swim);
        runBox = (CheckBox)findViewById(R.id.run);
        readBox = (CheckBox)findViewById(R.id.read);
        //为RadioGroup设置监听器,需要注意的是,这里的监听器和Button控件的监听器有所不同
        genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if(femaleButton.getId() == checkedId){
					System.out.println("famale");
					Toast.makeText(RadioTest.this, "famle", Toast.LENGTH_SHORT).show();
				}
				else if(maleButton.getId() == checkedId)
				{
					System.out.println("male");
				}
			}
		});
        
        //为多选按钮添加监听器
        swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
				{
					System.out.println("swim is checked");
				}
				else
				{
					System.out.println("swim is unchecked");
				}
			}
		});
        runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
				{
					System.out.println("run is checked");
				}
				else
				{
					System.out.println("run is unchecked");
				}
			}
		});
        readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
				{
					System.out.println("read is checked");
				}
				else
				{
					System.out.println("read is unchecked");
				}
			}
		});
    }
    
}

layout/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"
    >
<TextView
	android:id="@+id/textView1"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<RadioGroup
	android:id="@+id/genderGroup"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"
    >
    <RadioButton
    	android:id="@+id/femaleButton"
 	    android:layout_width="wrap_content" 
  	    android:layout_height="wrap_content" 
  	    android:text="@string/female"
  	    />
    <RadioButton
    	android:id="@+id/maleButton"
 	    android:layout_width="wrap_content" 
  	    android:layout_height="wrap_content" 
  	    android:text="@string/male"
  	    />
</RadioGroup>
<CheckBox
	android:id="@+id/swim"
 	android:layout_width="wrap_content" 
  	android:layout_height="wrap_content" 
  	android:text="@string/swim"
  	/>
<CheckBox
	android:id="@+id/run"
 	android:layout_width="wrap_content" 
  	android:layout_height="wrap_content" 
  	android:text="@string/run"
  	/>
<CheckBox
	android:id="@+id/read"
 	android:layout_width="wrap_content" 
  	android:layout_height="wrap_content" 
  	android:text="@string/read"
  	/>
</LinearLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的 Android 项目示例,其中包含三个页面布局、listview 控件和 Fragment: 首先,创建一个名为 "MyApp" 的新 Android 项目,然后在 res/layout 文件夹中创建三个 XML 布局文件和一个 Fragment 类。 1. activity_main.xml 布局文件 该布局文件包含一个文本框、一个编辑框、一个单选按钮和一个复选按钮。 ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter Your Name:" /> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Name" /> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/maleRadio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Male" /> <RadioButton android:id="@+id/femaleRadio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Female" /> </RadioGroup> <CheckBox android:id="@+id/checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Agree to terms and conditions" /> <Button android:id="@+id/nextButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Next" /> </LinearLayout> ``` 2. listview_layout.xml 布局文件 该布局文件包含一个 ListView 控件。 ```xml <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 3. fragment_layout.xml 布局文件 该布局文件包含一个文本框。 ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/textViewFragment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is a fragment" /> </LinearLayout> ``` 4. MyFragment.java Fragment 类 该类包含一个 onCreateView 方法,该方法返回 fragment_layout.xml 布局文件的根视图。 ```java public class MyFragment extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_layout, container, false); return rootView; } } ``` 最后,在 MainActivity.java 文件中实现整个应用程序逻辑。MainActivity 类继承了 AppCompatActivity 类,并且包含了三个页面布局和一个 Fragment。 ```java public class MainActivity extends AppCompatActivity { private EditText nameEditText; private RadioGroup genderRadioGroup; private CheckBox agreeCheckBox; private ListView listView; private Button nextButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取视图控件 nameEditText = findViewById(R.id.editText); genderRadioGroup = findViewById(R.id.radioGroup); agreeCheckBox = findViewById(R.id.checkBox); nextButton = findViewById(R.id.nextButton); listView = findViewById(R.id.listView); // 设置 ListView 适配器 String[] items = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}; ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items); listView.setAdapter(adapter); // 设置 Next 按钮点击事件 nextButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 获取用户输入 String name = nameEditText.getText().toString(); boolean isMale = genderRadioGroup.getCheckedRadioButtonId() == R.id.maleRadio; boolean agreed = agreeCheckBox.isChecked(); // 检查用户输入是否有效 if (name.isEmpty()) { Toast.makeText(MainActivity.this, "Please enter your name", Toast.LENGTH_SHORT).show(); return; } if (!agreed) { Toast.makeText(MainActivity.this, "Please agree to terms and conditions", Toast.LENGTH_SHORT).show(); return; } // 启动下一个活动 Intent intent = new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("name", name); intent.putExtra("isMale", isMale); startActivity(intent); } }); // 设置 ListView 点击事件 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // 创建并显示 Fragment FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); MyFragment fragment = new MyFragment(); fragmentTransaction.replace(R.id.fragment_container, fragment); fragmentTransaction.commit(); } }); } } ``` 这样,我们就完成了一个简单的 Android 应用程序,其中包含三个页面布局、listview 控件和 Fragment。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值