当前位置 : 主页 > 编程语言 > c++ >

Android新手写的小界面 欢迎高手赐教

来源:互联网 收集:自由互联 发布时间:2021-06-30
点击按钮会有相应的提示信息 写得比较粗糙 public class MainActivity extends AppCompatActivity { Button confirm,cancel; EditText usrname; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedIns
点击按钮会有相应的提示信息 写得比较粗糙
public class MainActivity extends AppCompatActivity {

    Button confirm,cancel;
    EditText usrname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        confirm = (Button) findViewById(R.id.confirmbtn);
        cancel = (Button) findViewById(R.id.cancelbtn);
        usrname = (EditText) findViewById(R.id.inputname);

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(usrname.getText().toString().isEmpty() == false) {
                    Toast.makeText(MainActivity.this, "Name " + usrname.getText().toString() + " confirmed.", Toast.LENGTH_LONG).show();
                }
                else{
                    Toast.makeText(MainActivity.this, "No confirmed.", Toast.LENGTH_LONG).show();
                }
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"Canceled.",Toast.LENGTH_LONG).show();
            }
        });

    }
}


 

 

    
  

    
  

    
   
    
    
  

    
  

    
  


 
MainActivity.java
package com.example.liugc.mysecondapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button confirm,cancel;
    EditText usrname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        confirm = (Button) findViewById(R.id.confirmbtn);
        cancel = (Button) findViewById(R.id.cancelbtn);
        usrname = (EditText) findViewById(R.id.inputname);

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(usrname.getText().toString().isEmpty() == false) {
                    Toast.makeText(MainActivity.this, "Name " + usrname.getText().toString() + " confirmed.", Toast.LENGTH_LONG).show();
                }
                else{
                    Toast.makeText(MainActivity.this, "No confirmed.", Toast.LENGTH_LONG).show();
                }
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"Canceled.",Toast.LENGTH_LONG).show();
            }
        });

    }
}
activity_main.xml
 

 

    
  

    
  

    
   
    
    
  

    
  

    
  


 
捕获.PNG
网友评论