簡體   English   中英

如何在 android 工作室中獲取按鈕的提示和文本

[英]How to get Hint and Text of a button In android studio

我正在使用此代碼獲取單擊的按鈕的 id,然后使用該 id 獲取同一按鈕的文本和提示我不知道為什么,但也許這段代碼在我使用 getID() 后會使應用程序崩潰方法。 我正在使用相對布局。

Java代碼

package com.example.firstapp;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity{

public AlertDialog.Builder dialogBuilder;
public AlertDialog dialog;
public EditText Name,Password;
public Button Save,Cancel,temp,ok,x;
public String file_location = "Passwords.txt";
public String name_x,pass_y;
public LinearLayout mylayout = null;
public RelativeLayout rl = null;


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

public void View_Password(Button x){
    ok = (Button) findViewById(R.id.button6);
    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();
        }
    });

};

public void NewPassword(){
    dialogBuilder = new AlertDialog.Builder(this);
    final View NewPassSaveView = getLayoutInflater().inflate(R.layout.newpasspopup, null);

    Name = (EditText) NewPassSaveView.findViewById(R.id.editTextTextPersonName);
    Password = (EditText) NewPassSaveView.findViewById(R.id.editTextTextPersonName2);

    Save = (Button) NewPassSaveView.findViewById(R.id.button4);
    Cancel = (Button) NewPassSaveView.findViewById(R.id.button);

    dialogBuilder.setView(NewPassSaveView);
    dialog = dialogBuilder.create();
    dialog.show();

   
    Save.setOnClickListener(new View.OnClickListener() {
        @SuppressLint("ResourceType")
        @Override
        public void onClick(View view) {
            if (TextUtils.isEmpty(Name.getText().toString())) {
                Name.setError("This Field is compulsory");
                return;
            } else if (TextUtils.isEmpty(Password.getText().toString())) {
                Password.setError("This Field is compulsory");
                return;
            }
            rl = (RelativeLayout) findViewById(R.id.layout2);
            Toast.makeText(getApplicationContext(), "Password Saving...", Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), "Password Saved", Toast.LENGTH_SHORT).show();
            Button temp = new Button(MainActivity.this);
            temp.setLayoutParams(new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT
            ));
            rl.addView(temp);
            temp.setText(Name.getText().toString());
            temp.setHint(Password.getText().toString());
            temp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int btn_id = v.getId();
                    btn2string(btn_id);
                }
            });
            dialog.dismiss();
        }
    });

    Cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();
        }
    });
}

public void AddNew(View view) {
    NewPassword();
}


public void btn2string(int x){
    Button btn = findViewById(x);
    name_x = btn.getText().toString();
    pass_y = btn.getHint().toString();
    See_Password(name_x,pass_y);
}

public void See_Password(String name,String pass){
    Toast.makeText(getApplicationContext(), "Name: "+name+"\n"+"Password: "+pass, Toast.LENGTH_SHORT).show();
}

}

//XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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:id="@+id/heading"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
    android:id="@+id/textView4"
    android:layout_width="319dp"
    android:layout_height="50dp"
    android:text="Password Manager"
    android:textSize="34sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.051" />
<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="28dp"
    android:layout_marginRight="28dp"
    android:onClick="AddNew"
    android:text="Add new"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView4"
    app:layout_constraintVertical_bias="0.973" />
<RelativeLayout
    android:id="@+id/layout2"
    android:layout_width="366dp"
    android:layout_height="541dp"
    android:layout_marginTop="28dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.488"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView4"
    app:layout_constraintVertical_bias="0.0"/>
 </androidx.constraintlayout.widget.ConstraintLayout>

請告訴是否有任何其他方法可以做到這一點或告訴上面代碼中的錯誤,以便我可以獲得單擊的那個按鈕的提示和文本......

謝謝你。

里面,

新密碼()

方法只需更改以下代碼。

public void NewPassword(){
 ......
 save.setOnClickListener(...){
    ........
    temp.setOnClickListener(new View.OnClickListener(){
       @Override
       public void onClick(View v){
          int btn_id = v.getId(); // remove this line.
          btn2String(temp);  //directly pass temp i.e. Button you created. 
         }
     });
      dialog.dismiss();
    }
  });
 ..........
}

並且,將 Button 類型作為參數傳遞給 btn2String() 方法。 如下:

public void btn2String(Button x){
  Button btn  = findViewById(x); // remove this line of code.
  name_x = x.getText().toString();
  pass_y = x.getHint().toString();
  See_Password(name_x, pass_y);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM