Android入门练习——获取匹配字符串(1)

本篇实现一个简单的综合小练习,主要涉及,鼠标单击、事件侦听、单选等,主要是想练习如何获取对应消息并显示出来。


实现如下图所示UI界面,当点击按钮后,“类别”和“金额”分别显示所选择和输入的内容。
在这里插入图片描述
Layout中:

<?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" 
    android:gravity="center_horizontal" >
    
     <LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="bottom"
    android:layout_marginTop="20dp">
    
       <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:textSize="15dp"/>
         
     </LinearLayout>

    <LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="bottom"
    android:layout_marginTop="10dp">
    
        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="类别:" 
        android:textSize="20dp"/>
        
       <RadioGroup
        android:id="@+id/rg1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="VIP" 
             android:textSize="20dp"/>

        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="普通" 
             android:textSize="20dp"/>
        
    </RadioGroup>  
    </LinearLayout>
        
<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="30dp"
    >
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="金额:" 
        android:gravity="center" 
         android:textSize="20dp"/>
        
        <EditText 
        android:id="@+id/et1"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:hint="输入金额"
        android:layout_marginLeft="20dp"
        android:gravity="center" 
        android:textSize="20dp"
        />
</LinearLayout>
	<LinearLayout 
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:orientation="horizontal"
	    android:layout_marginTop="30dp"
	    >
    
    <Button 
		     android:id="@+id/btn"
             android:layout_width="100dp"
             android:layout_height="wrap_content"
             android:text="确定"
             android:textSize="20dp"/>
	</LinearLayout>
</LinearLayout>

activity中:

public class MainActivity extends Activity {
	private Button btn;//声明
	private EditText et1;
	private RadioGroup rag1;
	private RadioButton rb1,rb2,rb3;
	private TextView mytv1;
	String str;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btn=(Button)findViewById(R.id.btn);//定义
		et1=(EditText) findViewById(R.id.et1);
		 rag1=(RadioGroup)findViewById(R.id.rg1);//绑定
	     rb1=(RadioButton)findViewById(R.id.rb1);
	     rb2=(RadioButton)findViewById(R.id.rb2);
	     mytv1=(TextView) findViewById(R.id.tv_result);
	     
	     rag1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
	    	 public void onCheckedChanged(RadioGroup group, int checkedId) {
					switch (checkedId) {
					case R.id.rb1:
						str="VIP";
						break;
					case R.id.rb2:
						str="普通";
						break;

					default:
						//mytv1.setText("新增");
						break;
					}}
		});
	  
        btn.setOnClickListener(new OnClickListener() {	
			@Override
			public void onClick(View v) {
				mytv1.setText("类别:"+str+"金额"+et1.getText().toString());
    
			}
        });
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43145926/article/details/89357572
今日推荐