安卓第三次作业

1.

package com.example.myapplication;


import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.text.AlteredCharSequence;
import android.view.Menu;

public class MainActivity extends Activity {

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

    }
    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        AlertDialog.Builder builder=new AlertDialog.Builder(this)
                .setTitle("普通对话框")
                .setMessage("是否退出至主菜单")
                .setIcon(R.drawable.ic_launcher_background)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                        MainActivity.this.finish();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });
        AlertDialog dialog=builder.create();
        dialog.show();
    }

}

 

2.

package com.example.chap6;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    private int num0=0;
    private int num1[]={0,1};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.bt).setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        AlertDialog.Builder builder=new AlertDialog.Builder(this)
                .setTitle("性别")
                .setSingleChoiceItems(new String[]{"男","女"}, -1, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        num0=which;
                    }
                })
                .setPositiveButton("是",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        if(num0==0){
                            Toast.makeText(MainActivity.this, "您选择的是男", Toast.LENGTH_LONG).show();
                        }else{
                            Toast.makeText(MainActivity.this, "您选择的是女", Toast.LENGTH_SHORT).show();
                        }
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("否", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });
        AlertDialog dialog=builder.create();
        dialog.show();
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

   <Button
       android:id="@+id/bt"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="请选择你的性别"
       android:textSize="25dp"
       android:layout_marginTop="32dp"
       android:layout_gravity="center"/>

</LinearLayout>

 

 

3.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#8BEC1B">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:text="1.TextView显示文本信息"
        android:textStyle="italic"
        android:textColor="#E71D1D"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="50dp"
        android:textSize="30sp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="60dp"
        android:layout_marginRight="20dp"
        android:text="2.按钮"
        android:textColor="#E71D1D"
        android:textSize="30sp" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="3.编辑框:请输入信息"
        android:textSize="30sp"
        android:layout_marginTop="120dp"
        android:textColor="#E71D1D"
        android:layout_marginLeft="35dp"/>
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="horizontal"/>
        <RadioButton
            android:id="@+id/rb_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4.男"
            android:textSize="30sp"
            android:layout_marginTop="180dp"
            android:textColor="#E71D1D"
            android:layout_marginLeft="35dp"/>
        <RadioButton
            android:id="@+id/rb_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:layout_toRightOf="@id/rb_1"
            android:textSize="30sp"
            android:layout_marginTop="180dp"
            android:textColor="#E71D1D"
            android:layout_marginLeft="35dp"/>
    <CheckBox
        android:id="@+id/cb_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="电脑"
        android:textColor="#E71D1D"
        android:textSize="30sp"
        android:layout_marginTop="230dp"
        android:layout_marginLeft="35dp"/>
    <CheckBox
        android:id="@+id/cb_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="手机"
        android:textColor="#E71D1D"
        android:textSize="30sp"
        android:layout_below="@id/cb_1"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="35dp"/>

</RelativeLayout>

.

 

 

4.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="120dp"
        android:layout_height="90dp"
        android:id="@+id/iv"
        android:layout_centerVertical="true"/>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/iv"
        android:layout_centerVertical="true">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/title"
            android:text="桌子"
            android:textSize="20sp"
            android:textColor="#551D1D"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_price"
            android:textSize="20sp"
            android:layout_marginTop="10dp"
            android:layout_below="@+id/title"
            android:textColor="#6B1919"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/price"
            android:text="1000"
            android:textSize="20sp"
            android:layout_below="@+id/title"
            android:layout_toRightOf="@+id/tv_price"
            android:textColor="#8A435B"
            android:layout_marginTop="10dp"/>

    </RelativeLayout>
</RelativeLayout>
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Main5Activity extends AppCompatActivity {
    private ListView mListView;
    private String[]titles={"桌子","苹果","蛋糕","线衣","猕猴桃","围巾"};
    private String[]prices={"1800元","10元/kg","300元","350元","10元/kg","280元"};
    private int []icons={R.drawable.table,R.drawable.apple,R.drawable.cake,R.drawable.wireclothes,R.drawable.kiwifruit,R.drawable.scarf};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);
        mListView=(ListView)findViewById(R.id.lv);
        MyBaseAdapter mAdapter=new MyBaseAdapter();
        mListView.setAdapter(mAdapter);
    }
    class MyBaseAdapter extends BaseAdapter{
        @Override
        public int getCount(){
            return titles.length;
        }
        @Override
        public Object getItem(int position){
            return titles[position];
        }
        @Override
        public long getItemId(int position){
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            View view=View.inflate(Main5Activity.this,R.layout.list_item,null);
            TextView title=(TextView)view.findViewById(R.id.title);
            TextView price=(TextView)view.findViewById(R.id.price);
            ImageView iv=(ImageView)view.findViewById(R.id.iv);
            title.setText(titles[position]);
            price.setText(prices[position]);
            iv.setBackgroundResource(icons[position]);
            return  view;

        }

 

posted @ 2021-10-07 19:37  MaLD  阅读(9)  评论(0编辑  收藏  举报