简体   繁体   English

Android在单击按钮时显示弹出消息或动画

[英]Android displaying pop-up message or animation on button click

I am really new to android programming, and I'm stuck with a really small problem in my app. 我真的是android编程的新手,并且我的应用程序遇到了一个很小的问题。 The (very simple) app is almost ready, its just that there is a button and when it is clicked, I want the user to get a response in the form of either a message or some animation so that the user can feel that the app has registered the button click. (非常简单的)应用程序几乎准备就绪,只是有一个按钮,当单击它时,我希望用户以消息或某些动画的形式获得响应,以便用户可以感觉到该应用程序已注册按钮单击。 I've been looking up and trying stackoverflow and other tutorials but to no avail. 我一直在寻找并尝试stackoverflow和其他教程,但无济于事。 I just want some kind of a response when the button is clicked, so that the user doesn't feel confused about whether the app is working or not. 我只想在单击按钮时做出某种响应,以使用户不会对应用程序是否正常工作感到困惑。 Any help will be greatly appreciated!! 任何帮助将不胜感激!! Here's the code below:- 这是下面的代码:

The XML file:- XML档案:-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MyActivity"
    android:background="#d1000000">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me"
        android:id="@+id/Hinglishbutton"
        android:background="#cfa16cff"
        android:hapticFeedbackEnabled="true"
        android:onClick="buttonHandler"
        android:clickable="true"
        android:textColor="#a5fafbf9"
        tools:ignore="HardcodedText"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="71dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Welcome to Hinglish :)"
        android:id="@+id/textView4"
        android:textColor="#a5fafbf9"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="62dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hinglish lets you add Hindi words to your predictive dictionary thus enabling a richer typing experience!"
        android:id="@+id/textView"
        android:textColor="#a5fafbf9"
        android:textIsSelectable="false"
        android:textSize="14sp"
        android:typeface="sans"
        android:layout_marginTop="80dp"
        android:layout_below="@+id/textView4"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click the button once, minimize Hinglish and go grab a coffee. You will now be able to type hindi words without autocorrect converting them to junk english!"
        android:id="@+id/textView2"
        android:textColor="#a5fafbf9"
        android:textIsSelectable="false"
        android:textSize="14sp"
        android:typeface="sans"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Hosting this app costs money! To pitch in, contact the developer at spandan.madan@gmail.com :) :)"
        android:id="@+id/textView5"
        android:textColor="#a5fafbf9"
        android:layout_below="@+id/textView2"
        android:layout_alignRight="@+id/textView2"
        android:layout_alignEnd="@+id/textView2"
        android:layout_marginTop="43dp" />


</RelativeLayout>

The myActivity.java file:- myActivity.java文件:-

   package com.example.spandanmadan1.hinglish;

import android.content.res.AssetManager;
import android.provider.UserDictionary;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class MyActivity extends ActionBarActivity {

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


        Button bsubmit = (Button) findViewById(R.id.Hinglishbutton);

        bsubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "Thank you for using Hinglish", Toast.LENGTH_LONG).show();
                InputStream fis = getResources().openRawResource(R.raw.hindislang);
                BufferedReader bfr = null;
                try {
                    bfr = new BufferedReader(new InputStreamReader(fis));
                    String line = null;
                    while ((line = bfr.readLine()) != null) {
                        UserDictionary.Words.addWord(getApplicationContext(), line, 1, "", null);
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

I created a new project, used your xml and in the buttonHandler function I gave the toast as follows: 我创建了一个新项目,使用了您的xml,并在buttonHandler函数中按如下所示进行了祝酒:

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;


public class MyActivity extends ActionBarActivity {

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

public void buttonHandler(View view) {
    Toast.makeText(getApplicationContext(), "Thank you for using Hinglish", Toast.LENGTH_LONG).show();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_my, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

This works. 这可行。 The Toast : "Thank you for using Hinglish" was displayed on screen. 屏幕上显示Toast:“谢谢您使用英语”。

You can use a Toast: 您可以使用Toast:

Toast.makeText(getApplicationContext(),"<Your message to the User>",Toast.LENGTH_SHORT).show();

The message will be shown when you click the button. 单击该按钮时,将显示该消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM